這樣做的目的是允許用戶在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();
}
你得到一個異常?如果不是,那麼我們也需要存儲過程的內容。 – David
'錯誤',錯誤是......? – tnw
什麼'Int32 newId =(Int32)myCommand.ExecuteScalar();'做什麼? – rt2800