C# 在ASP.NET中的SQL update上设置多个值
我有一个非常简单的ASPX页面,它有一个文本框和一个按钮。当用户输入[ID]号时,它会自动将日期/时间戳添加到SQL中相应的行[ShipDate]。现在,除此之外,我需要做的是设置另一列[ScanQuantity](和相应的行)来计算提交该ID的次数。因此,第一次提交时,它将相应的行和列设置为1,下一次为2,因此需要递增1 我不确定我的代码是否真的有效,但它确实有效。现在,当该人员输入[ID]且[ShipDate]为空时,它会向[ShipDate]添加一个日期/时间戳。如果一个人扫描的[ID]在[ShipDate]中已经有一个值,它会告诉他们它已经被标记为已发货。如果扫描的[ID]在系统中找不到,它会告诉他们。在前两个场景中,当没有发货日期值时,甚至当它有发货日期值时,我需要递增计数 非常感谢您的帮助C# 在ASP.NET中的SQL update上设置多个值,c#,sql,asp.net,C#,Sql,Asp.net,我有一个非常简单的ASPX页面,它有一个文本框和一个按钮。当用户输入[ID]号时,它会自动将日期/时间戳添加到SQL中相应的行[ShipDate]。现在,除此之外,我需要做的是设置另一列[ScanQuantity](和相应的行)来计算提交该ID的次数。因此,第一次提交时,它将相应的行和列设置为1,下一次为2,因此需要递增1 我不确定我的代码是否真的有效,但它确实有效。现在,当该人员输入[ID]且[ShipDate]为空时,它会向[ShipDate]添加一个日期/时间戳。如果一个人扫描的[ID]在
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(GetConnectionString()))
{
try
{
connection.Open();
string sql = @"SELECT ID from ProductTracking where ID = '" + TextBox1.Text + "' and ShipDate is null";
using(SqlCommand command = new SqlCommand(sql, connection))
{
using(SqlDataReader reader = command.ExecuteReader())
{
if(reader.HasRows)
{
string sql2 = @"UPDATE [ProductTracking] SET ShipDate=@Value2 where ID=@Value1";
using (SqlCommand command2 = new SqlCommand(sql2, connection))
{
command2.Parameters.AddWithValue("@Value1", TextBox1.Text);
command2.Parameters.AddWithValue("@Value2", DateTime.Now);
command2.ExecuteNonQuery();
}
pageBody.Attributes.Add("bgcolor", "#9aff8e");
Label1.Text = "Item " + TextBox1.Text + " Recorded!";
TextBox1.Text = "";
}
else
{
reader.Close();
string sql3 = @"SELECT ID from ProductTracking where ID = '" + TextBox1.Text + "'";
using(SqlCommand command3 = new SqlCommand(sql3, connection))
{
using(SqlDataReader reader2 = command3.ExecuteReader())
{
if (reader2.HasRows)
{
pageBody.Attributes.Add("bgcolor", "#fbff8e");
Label1.Text = "Item " + TextBox1.Text + " Already Shipped!";
TextBox1.Text = "";
}
else
{
pageBody.Attributes.Add("bgcolor", "#ff8e8e");
Label1.Text = "Item " + TextBox1.Text + " Not Found!";
TextBox1.Text = "";
}
}
}
}
}
}
}
finally
{
if(connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
}
}
您可以将其简化为一次数据库访问。即使您必须在同一行程中运行两个命令,这也会更好
protected void Button1_Click(object sender, EventArgs e)
{
string sql = @"SELECT ShipDate FROM [ProductTracking] WHERE ID= @ProductID;UPDATE [ProductTracking] SET ShipDate = current_timestamp, ScanQty = coalesce(ScanQty,0) + 1 WHERE ID= @ProductID";
using (var connection = new SqlConnection(GetConnectionString()))
using (var command = new SqlCommand(sql, connection))
{
command.Parameters.Add("@ProductID", SqlDbType.Int).Value = TextBox1.Text;
connection.Open();
using (SqlDataReader rdr = command.ExecuteReader())
{
if (!rdr.Read())
{
pageBody.Attributes.Add("bgcolor", "#ff8e8e");
Label1.Text = "Item " + TextBox1.Text + " Not Found!";
}
else if (rdr["ShipDate"] == DBNull.Value)
{
pageBody.Attributes.Add("bgcolor", "#9aff8e");
Label1.Text = "Item " + TextBox1.Text + " Recorded!";
}
else
{
pageBody.Attributes.Add("bgcolor", "#fbff8e");
Label1.Text = "Item " + TextBox1.Text + " Already Shipped!";
}
rdr.Close();
}
}
TextBox1.Text = "";
}
我还想知道为什么原始代码在演示参数化查询时使用字符串连接。使用字符串连接将值放入SQL查询中是不合适的,即使该值只是一个int。您知道有些人还没有遇到参数化查询,但一旦您知道如何使用它们,在其他地方继续进行字符串连接是不专业的。您到底想要什么?整个故事很难理解。
使用
块的关键是你不再需要最终
。