这样做的目的是允许用户在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