当用户在页面上的三个框中输入特定值时,我有代码将数据插入表中。
这些方框是订单号,总重量和跟踪参考。
我现在需要为此代码添加更多功能,首先检查订单号是否存在,如果需要更新列,如果不是,我需要插入新行并添加数据这一点。
我在想,就像IF结果= 0,插入NEW,ELSE更新
如何修改我的代码才能执行此操作?
protected void Page_Load(object sender, EventArgs e)
{
errorLabel.Visible = false;
successLabel.Visible = false;
errorPanel.Visible = false;
}
protected void submitBtn_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
int _orderID = Convert.ToInt32(orderID.Text);
string _trackingID = trackingNumber.Text;
DateTime _date = DateTime.UtcNow;
int _weightID = Convert.ToInt32(weightID.Text);
SqlConnection myConnection = new SqlConnection("Data Source=localhost\\Sqlexpress;Initial Catalog=databasename;User ID=username;Password=password");
SqlCommand myCommand = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
try
{
myConnection.Open();
myCommand.Parameters.AddWithValue("@order", _orderID);
myCommand.Parameters.AddWithValue("@tracking", _trackingID);
myCommand.Parameters.AddWithValue("@date", _date);
myCommand.Parameters.AddWithValue("@weight", _weightID);
int rowsUpdated = myCommand.ExecuteNonQuery();
myConnection.Close();
if (rowsUpdated > 0)
{
alertdiv.Attributes.Add("class", "alert alert-success form-signin");
successLabel.Text = "Thank you, tracking details have been updated";
successLabel.Visible = true;
errorPanel.Visible = true;
}
else
{
alertdiv.Attributes.Add("class", "alert alert-error form-signin");
errorLabel.Text = "Oh dear, the order number is not recognised, please check and try again";
errorLabel.Visible = true;
errorPanel.Visible = true;
}
orderID.Text = "";
trackingNumber.Text = "";
weightID.Text = "";
}
catch (Exception f)
{
errorLabel.Text = "This order number does not exist, please check";
errorLabel.Visible = true;
errorPanel.Visible = true;
return;
}
}
}
protected void Signout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Login.aspx");
}
答案 0 :(得分:7)
您可以在SELECT
声明之前添加一些INSERT
查询。因此,如果SELECT
查询返回多行,则表示您已在数据库中拥有该记录,并且需要更新。所以,一般情况下它会像
SqlCommand cmdCount = new SqlCommand("SELECT count(*) from Shipment WHERE OrderId = @order", myConnection);
cmdCount.Parameters.AddWithValue("@order", _orderID);
int count = (int)cmdCount.ExecuteScalar();
if (count > 0)
{
// UPDATE STATEMENT
SqlCommand updCommand = new SqlCommand("UPDATE Shipment SET TrackingNumber = @tracking, ShippedDateUtc = @date, TotalWeight = @weight", myConnection);
updCommand.Parameters.AddWithValue("@order", _orderID);
updCommand.Parameters.AddWithValue("@tracking", _trackingID);
updCommand.Parameters.AddWithValue("@date", _date);
updCommand.Parameters.AddWithValue("@weight", _weightID);
int rowsUpdated = myCommand.ExecuteNonQuery();
}
else
{
// INSERT STATEMENT
SqlCommand insCommand = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
insCommand.Parameters.AddWithValue("@order", _orderID);
insCommand.Parameters.AddWithValue("@tracking", _trackingID);
insCommand.Parameters.AddWithValue("@date", _date);
insCommand.Parameters.AddWithValue("@weight", _weightID);
int rowsUpdated = myCommand.ExecuteNonQuery();
}
修改强> 或者更短:
SqlCommand command;
if (count > 0)
{
command = new SqlCommand("UPDATE Shipment SET TrackingNumber = @tracking, ShippedDateUtc = @date, TotalWeight = @weight WHERE OrderId = @order", myConnection);
}
else
{
command = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
}
command.Parameters.AddWithValue("@order", _orderID);
command.Parameters.AddWithValue("@tracking", _trackingID);
command.Parameters.AddWithValue("@date", _date);
command.Parameters.AddWithValue("@weight", _weightID);
int rowsUpdated = command.ExecuteNonQuery();
答案 1 :(得分:3)
最有效的方法是将功能放入存储过程,例如(伪代码):
IF EXISTS(SELECT * FROM Orders WHERE OrderNo = @orderNo)
UPDATE ...
ELSE
INSERT ...
如果无法创建新的存储过程,也可以创建包含此Statement的命令,但可读性通常更差。
这两种方法只需要一个DB请求。