您不能使用浮点整数等值,例如文本字段:
String query = "UPDATE Table_com SET Contents='" + textBox6.Text + "' WHERE Variable='" + comboBox5.Text + "'";
并且您的代码确实存在风险,您可以使用像这样的参数,不要使用字符串,并将值转换为float:
static void Main(string[] args)
{
var ConnectionString = "YOUR CONNECTION STRING";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConnectionString))
{
String query = "UPDATE Table_com SET Contents=@contents WHERE Variable=@variable";
using (SqlCommand cmd = new SqlCommand(query,con))
{
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add("@contents",SqlDbType.Float).Value = Convert.ToDouble(textBox6.Text);
cmd.Parameters.Add("@variable",SqlDbType.NVarChar).Value = comboBox5.Text;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
}
,
这是我尝试引用Wiki可以回答的问题
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-VEFPLGG\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
private void comboBox5_SelectedIndexChanged(object sender,EventArgs e)
{
DataTable dt = new DataTable();
String query = "UPDATE Table_com SET Contents=@contents ";
using (SqlCommand cmd = new SqlCommand(query,con))
{
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add("@contents",SqlDbType.Float).Value = textBox6.Text;
//cmd.Parameters.Add("@variable",SqlDbType.Float).Value = Convert.ToDouble(comboBox5.Text);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
本文链接:https://www.f2er.com/3110660.html