這是我的編碼我在我的代碼中遇到了這種型別的錯誤
(拋出例外:System.Data.dll 中的“System.Data.SqlClient.SqlException”
附加資訊:將資料型別 varchar 轉換為 bigint 時出錯
更新查詢:___________________________________
private void btnUpdate_Click(object sender, EventArgs e)
{
query = ("update items set name='" txtName.Text "',category='" txtCategory.Text "',price='" txtPrice.Text "where iid =" id "'");
fn.setData(query);
loadData();
txtName.Clear();
txtCategory.Clear();
txtPrice.Clear();
}
設定查詢_______________
public void setData(String query)
{
SqlConnection con = getConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = query;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Processed Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
uj5u.com熱心網友回復:
始終嘗試使用引數化查詢或存盤程序,而不是注入值。
btnUpdate_Click
private void btnUpdate_Click(object sender, EventArgs e)
{
query = ("update items set name = @name, category = @category, price = @price where iid = @id");
fn.setData(query,long.Parse(id),txtName.Text, txtCategory.Text, long.Parse(txtPrice.Text));
loadData();
txtName.Clear();
txtCategory.Clear();
txtPrice.Clear();
}
setData function
public void setData(String query, long id,string name, string category, long price)
{
SqlConnection con = getConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@category", category);
cmd.Parameters.AddWithValue("@price", price);
cmd.Parameters.Add(new SqlParameter()
{
DbType = System.Data.DbType.Int64, //For big int
Direction = System.Data.ParameterDirection.Input,
ParameterName = "@id",
Value = id
});
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Processed Successfully.", "Success",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex)
{
// catch exception here
}
}
uj5u.com熱心網友回復:
如下所示更改您的代碼,它應該可以解決您的問題。
{
query = ("更新專案集名稱='" txtName.Text "',category='" txtCategory.Text "',price= " int.Parse(txtPrice.Text) "where iid=" id "'");
fn.setData(query);
loadData();
txtName.Clear();
txtCategory.Clear();
txtPrice.Clear();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/503938.html
標籤:C# 网 asp.net-mvc asp.net 核心 c#-2.0
