[英]How to update SQL Table Using DataGridView Values in C#?
enter image description here I tried to update selected columns of SQL Database table which from DataGridView. 在这里输入图像描述,我尝试从DataGridView更新SQL数据库表的选定列。 But it said my input string is wrong.So how to fix this.(PO_No is the primary key of PO table and it has identity value and also it is the foreign key of PO_Cart table)
但是它说我的输入字符串是错误的,那么如何解决这个问题(PO_No是PO表的主键,它具有标识值,也是PO_Cart表的外键)
public void UpdatePOCartTable(int PO_No,string ISBN_No,int OrderQuantity, decimal UnitPrice, decimal Total)
{
DynamicConnection con = new DynamicConnection();
con.mysqlconnection();
string query = "UPDATE TBL_PO_Cart"
+ " SET ISBN_No = @ISBN_No, OrderQuantity= @OrderQuantity,"
+ "UnitPrice= @UnitPrice, Total=@Total"
+ "WHERE PO_No = @PO_No";
con.sqlquery(query);
con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
con.cmd.Parameters["@PO_No"].Value = PO_No;
con.cmd.Parameters.Add(new SqlParameter("@ISBN_No", SqlDbType.NVarChar));
con.cmd.Parameters["@ISBN_No"].Value = ISBN_No;
con.cmd.Parameters.Add(new SqlParameter("@OrderQuantity", SqlDbType.NVarChar));
con.cmd.Parameters["@OrderQuantity"].Value = OrderQuantity;
con.cmd.Parameters.Add(new SqlParameter("@UnitPrice", SqlDbType.Money));
con.cmd.Parameters["@UnitPrice"].Value = UnitPrice;
con.cmd.Parameters.Add(new SqlParameter("@Total", SqlDbType.Money));
con.cmd.Parameters["@Total"].Value = Total;
con.nonquery();
}
private void btnedit_Click(object sender, EventArgs e)
{
DynamicConnection con = new DynamicConnection();
try
{
if (txtPONo.Text != "" || cmbsupID.Text != "" || date1.Text != "" || requireddate.Text != "" || txtgrandTotal.Text != "")
{
PurchaseOrder PO = new PurchaseOrder();
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
PO.UpdatePOCartTable(Convert.ToInt32(txtPONo.Text),dataGridView1.Rows[i].Cells[1].Value.ToString(), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value.ToString()), Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value.ToString()));
}
}
else
{
MessageBox.Show("Please Provide Details!");
}
dataGridView1.Rows.Clear();
ClearData();
retviewPO_No();
MessageBox.Show("Record Updated Successfully");
}
catch (Exception ex)
{
MessageBox.Show("Error Occured" + ex.Message);
}
}
When Updating SQL I normally use the following code: 更新SQL时,我通常使用以下代码:
String CommandLine = "UPDATE CA_temp_data SET TimePast=@selectTot WHERE TicketNumber=@id";
using (SqlConnection Conn = new SqlConnection("Data Source=" + hostString + ";User ID=" + usernamestring + ";Password=" + sqlpassword))
{
try
{
SqlCommand cmd = new SqlCommand(CommandLine, Conn);
cmd.Parameters.AddWithValue("@id", ticket);
cmd.Parameters.AddWithValue("@selectTot", time);
using (Conn)
{
Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();
}
}
catch (System.Exception excep)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.