这样做的目的是允许用户在winform中输入数据,然后它会在成功时在消息框中返回主键。
但是,在调试模式下运行时遇到错误,似乎记录无法插入数据库。有什么帮助吗?
我检查了数据库连接是否正常,存储过程已经过测试。系统方面,带有 SQL Server 2008 r2 标准的 Visual Studio 2010
代码如下:
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();
}