我一直收到错误消息
System.Data.SqlClient.SqlException:'必须声明标量变量" @ ID"。' -
我有一个gridview,可以捕获用户输入文本框的内容,并通过单击按钮将其提交到数据库。当用户单击该按钮时,我还尝试将当前登录的用户存储到数据库表中。有人可以帮助我为什么这个消息一直在发生?
以下是我的按钮点击事件的C#代码
protected void btnSubmit_Click(object sender, EventArgs e)
{
string query = "insert into Stock_Take_Item(ItemID, BarQuantity, StorageQuantity) values(@ID, @BAR, @STORAGE); insert into Stock_Take(Username, StockDate)" +
" values(@Username, GetDate())";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@Username", Session["username"].ToString());
foreach (GridViewRow row in gvStockTake.Rows)
{
Label ID = row.FindControl("itemId") as Label;
TextBox BAR = row.FindControl("txtBar") as TextBox;
TextBox STORAGE = row.FindControl("txtStorage") as TextBox;
cmd.Parameters.Clear();
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
cmd.Parameters.Add("@BAR", SqlDbType.Int).Value = BAR.Text;
cmd.Parameters.Add("@STORAGE", SqlDbType.Int).Value = STORAGE.Text;
cmd.ExecuteNonQuery(); //query execution
}
con.Close();
Response.Write("Successfully inserted stock take items.");
}
以下是我登录C#代码的代码
protected void btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection sqlCon = new SqlConnection(@"Data Source=(local)\;Initial Catalog=SmallBatch;Integrated Security=True;"))
{
sqlCon.Open();
string query = "SELECT COUNT(1) FROM Site WHERE Username=@username AND Password=@password";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.Parameters.AddWithValue("@username", txtUsername.Text.Trim());
sqlCmd.Parameters.AddWithValue("@password", txtPassword.Text.Trim());
int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
if (count == 1)
{
Session["username"] = txtUsername.Text.Trim();
Response.Redirect("Dashboard.aspx");
}
else
{
lblErrorMessage.Visible = true;
}
}
}
我还是ASP.NET和C#的新手,所以任何建议都非常感谢!
答案 0 :(得分:0)
我会批量做事 - 它会减少到数据库的次数
protected void btnSubmit_Click(object sender, EventArgs e)
{
var counter = 0;
var query = @"insert into Stock_Take_Item
(ItemID, BarQuantity, StorageQuantity)
values
({1}, {2}, {3});
insert into Stock_Take
(Username, StockDate)
values
({0}, GetDate());";
var paramList = new List<SqlParameter>();
var sqlBulk = new StringBuilder(10000);
var p0 = "@0";
var maxRows = 100;
var currRow = 0;
var totalRows = 0;
foreach (GridViewRow row in gvStockTake.Rows)
{
Label ID = row.FindControl("itemId") as Label;
TextBox BAR = row.FindControl("txtBar") as TextBox;
TextBox STORAGE = row.FindControl("txtStorage") as TextBox;
currRow++;
totalRows++;
if (counter == 0)
{
paramList.Add(new SqlParameter(, Session["username"].ToString()));
}
var p1 = "@" + ++counter;
var p2 = "@" + ++counter;
var p3 = "@" + ++counter;
paramList.AddRange(new[]{
new SqlParameter(p1, Convert.ToInt32(Id.Text),
new SqlParameter(p2, Convert.ToInt32(BAR.Text),
new SqlParameter(p3, Convert.ToInt32(STORAGE.Text)
});
sqlBulk.AppendFormat(query, p0, p1, p2, p3);
if (currRow == maxRows || totalRows == gvStockTake.Rows)
{
using (var con = new SqlConnection(connectionString))
{
using (var cmd = new SqlCommand(sqlBulk.ToString(), con))
{
cmd.Parameters.AddRange(paramList);
con.Open();
cmd.ExecuteNonQuery();
}
}
currRow = 0;
counter = 0;
paramList.Clear();
sqlBulk.Length = 0;
}
}
}
免责声明:未经测试。