2013-09-27 27 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