我是 C# 编程的新手,所以如果有人可以帮助我,我将不胜感激。我知道有类似的问题,但我仍然找不到我的问题的解决方案。我正在开发一个模拟系统,当用户购买产品时,系统将存储所有交易细节。问题是,我无法将数据插入数据库。这是代码:
using (SqlConnection conn = new SqlConnection
(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
string QueryA = "@Insert into TransDetails(AccountNumber,Amount,Provider"
+ ",Mobile Number,TransNum,TransDate, Status) "
+ " Values (@AccountNumber,@Amount,@Provider,@Mobile Number,"
+ "@TransNum,@TransDate,@Status";
using (SqlCommand cmd = new SqlCommand("InsertRecord", conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = QueryA;
cmd.Parameters.AddWithValue("@AccountNumber", acc.Text);
cmd.Parameters.AddWithValue("@Amount", lblAmount.Text);
cmd.Parameters.AddWithValue("@Provider", lblProvider.Text);
cmd.Parameters.AddWithValue("@Mobile Number", lblNumber.Text);
cmd.Parameters.AddWithValue("@TransNum", lblTrans.Text);
cmd.Parameters.AddWithValue("@TransDate", lblDate.Text);
cmd.Parameters.AddWithValue("@Status", status.Text);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch
{
lblMessage.Text = "Error";
}
finally
{
conn.Close();
}
}
}
门店流程如下:
ALTER PROCEDURE InsertRecord1
@AccountNumber int,
@Amount nchar(10),
@Provider nchar(10),
@MobileNumber int,
@TransNum nchar(10),
@TransDate date,
@Status nchar(10)
AS
Insert into TransDetails(AccountNumber,Amount,Provider,MobileNumber,TransNum,TransDate,Status)
Values (@AccountNumber,@Amount,@Provider,@MobileNumber,@TransNum,@TransDate,@Status)
return
非常感谢任何帮助。P/S:我不知道为什么存储过程的开头以“alter”开头。