2013-09-27 23 views
-2

这样做的目的是允许用户在winform中输入数据,然后它会在成功时返回消息框中的主键。无法在c#中的窗体中插入记录#

但是,我在调试模式下运行时遇到错误,似乎该记录无法插入到数据库中。任何帮助?

我检查了数据库连接是否正常,存储过程被测试过。系统明智时,Visual Studio 2010和SQL Server 2008 R2的标准

的代码如下:

try 
    { 
     myConnection.Open(); 
    } 
    catch (Exception error) 
    { 
     MessageBox.Show("Unable to connection to database " + error.ToString()); 
    } 

    try 
    { 
     DateTime date = dtDate.Value.Date; 
     string CommentSite = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
     string Revisit = cboRevisit.Text; 
     string CustomerGroup = cboCustomerGroup.Text; 
     string CommentText = txtComment.Text; 
     int CommentValue1 = Int32.Parse(cbo1.Text); 
     int CommentValue2 = Int32.Parse(cbo2.Text); 
     int CommentValue3 = Int32.Parse(cbo3.Text); 
     int CommentValue4 = Int32.Parse(cbo4.Text); 
     int CommentValue5 = Int32.Parse(cbo5.Text); 
     int CommentValue6 = Int32.Parse(cbo6.Text); 
     int CommentValue7 = Int32.Parse(cbo7.Text); 
     int CommentValue8 = Int32.Parse(cbo8.Text); 

     SqlCommand myCommand = new SqlCommand(@"InsertCommentData", myConnection); 
     myCommand.CommandType = CommandType.StoredProcedure; 
     //myCommand.Parameters.Add("@WeekEnding", SqlDbType.DateTime); 
     myCommand.Parameters.Add(new SqlParameter("@CommentDate", date)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentSite", CommentSite)); 
     myCommand.Parameters.Add(new SqlParameter("@Revisit", Revisit)); 
     myCommand.Parameters.Add(new SqlParameter("@CusomterGroup", CustomerGroup)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentText", CommentText)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue1", CommentValue1)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue2", CommentValue2)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue3", CommentValue3)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue4", CommentValue4)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue5", CommentValue5)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue6", CommentValue6)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue7", CommentValue7)); 
     myCommand.Parameters.Add(new SqlParameter("@CommentValue8", CommentValue8)); 
     myCommand.ExecuteNonQuery(); 
     Int32 newId = (Int32) myCommand.ExecuteScalar(); 

     MessageBox.Show("Record saved! ID: " + newId + "\n" + "Please write down the number in the card top right hand corner"); 

    } 
    catch (Exception error) 
    { 
     MessageBox.Show("Unable to add/update record " + error.ToString()); 
    } 
    finally 
    { 
     myConnection.Close(); 
    } 

enter image description here enter image description here

+0

你得到一个异常?如果不是,那么我们也需要存储过程的内容。 – David

+1

'错误',错误是......? – tnw

+0

什么'Int32 newId =(Int32)myCommand.ExecuteScalar();'做什么? – rt2800

回答

3

的错误似乎非常简单:你不必EXECUTE权限此存储过程。更具体地说,您运行应用程序的用户帐户缺少它。显然,在你的测试环境中,你确实拥有特权 - 或者至少是谁运行测试的人。

为了让这样的访问,数据库所有者可以运行此:

GRANT EXECUTE ON InsertCommentData TO [user name] 
+0

它的工作原理,但我必须注释掉Int32 newId =(Int32)myCommand.ExecuteScalar();为了使它工作,我如何检索C#中的新主键? – user549410

+0

我需要看到sproc的内部进行猜测。当我有一个创建记录的过程时,我通常会在“OUT”参数中传回新ID而不是返回值。如果你喜欢,我可以粘贴示例代码。 – 2013-09-27 17:22:30