任何人都可以告诉我错误在哪里,我在cmd.ExecuteNonquery()处获得错误,即'varchar'附近的语法不正确。 'Type'附近的语法不正确。
string CS = ConfigurationManager.ConnectionStrings["RelayTestMachine.Properties.Settings.RelayTestConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("INSERT INTO TestValue (Relay Type,Applied Voltage) VALUES(@Relay Type,@Applied Voltage)",con);
cmd.Parameters.Add("@Relay Type", SqlDbType.VarChar).Value = tbRelayType.Text;
if (rbtn100.Checked == true)
{
cmd.Parameters.Add("@Applied Voltage", SqlDbType.VarChar).Value = rbtn100.Text;
}
if (rbtn75.Checked == true)
{
cmd.Parameters.Add("@Applied Voltage", SqlDbType.VarChar).Value = rbtn75.Text;
}
con.Open();
cmd.ExecuteNonQuery();
}
答案 0 :(得分:2)
尝试使用此插入语句
INSERT INTO TestValue ([Relay Type],[Applied Voltage]) VALUES(@RelayType,@AppliedVoltage)
即,您需要从变量名中删除空格并将列名放在[]
中答案 1 :(得分:0)
从变量名中删除空格,并将[]
添加到列名称。
变量名和列名不允许使用空格 由中间的空格组成,然后必须用
[]
或""
括起来INSERT INTO TestValue ([Relay Type],[Applied Voltage]) VALUES(@RelayType,@AppliedVoltage)"
string CS = ConfigurationManager.ConnectionStrings["RelayTestMachine.Properties.Settings.RelayTestConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("INSERT INTO TestValue ([Relay Type],[Applied Voltage]) VALUES(@RelayType,@AppliedVoltage)",con);
cmd.Parameters.Add("@RelayType", SqlDbType.VarChar).Value = tbRelayType.Text;
if (rbtn100.Checked == true)
{
cmd.Parameters.Add("@AppliedVoltage", SqlDbType.VarChar).Value = rbtn100.Text;
}
if (rbtn75.Checked == true)
{
cmd.Parameters.Add("@AppliedVoltage", SqlDbType.VarChar).Value = rbtn75.Text;
}
con.Open();
cmd.ExecuteNonQuery();
}
整体就像这样
user == null