'varchar' 附近的语法不正确。 'Type' 附近的语法不正确
Incorrect syntax near 'varchar'. Incorrect syntax near 'Type'
谁能告诉我错误在哪里,我在 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();
}
从变量名中删除空格并在列名中添加 []
。
White Spaces are not allowed in variable name and if column names
consist of spaces in between then they must be enclosed with []
or
""
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();
}
尝试使用这个插入语句
INSERT INTO TestValue ([Relay Type],[Applied Voltage]) VALUES(@RelayType,@AppliedVoltage)
即,您需要从变量名中删除空格并将列名放在 []
中
谁能告诉我错误在哪里,我在 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();
}
从变量名中删除空格并在列名中添加 []
。
White Spaces are not allowed in variable name and if column names consist of spaces in between then they must be enclosed with
[]
or""
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();
}
尝试使用这个插入语句
INSERT INTO TestValue ([Relay Type],[Applied Voltage]) VALUES(@RelayType,@AppliedVoltage)
即,您需要从变量名中删除空格并将列名放在 []
中