我的数据库列名是"从"但我收到错误,而我将列名更改为其他名称错误未发生.为什么?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
con.Open();
string qry="insert into Mynew(name,age,from,address,job)values(@name,@age,@from,@address,@job)";
SqlCommand cmd= new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@age", TextBox2.Text);
cmd.Parameters.AddWithValue("@from",TextBox3.Text);
cmd.Parameters.AddWithValue("@address", TextBox4.Text);
cmd.Parameters.AddWithValue("@job", TextBox5.Text);
cmd.ExecuteNonQuery();
con.Close();
}
Run Code Online (Sandbox Code Playgroud)
当我将列名更改为城市时我没有收到错误为什么会发生?
from是SQL中的保留关键字.如果要将其用作表名,则必须通过以下方式将其转义:
所以要么
insert into Mynew(name,age,`from`,address,job)values(@name,@age,@from,@address,@job)
Run Code Online (Sandbox Code Playgroud)
要么
insert into Mynew(name,age,[from],address,job)values(@name,@age,@from,@address,@job)
Run Code Online (Sandbox Code Playgroud)
取决于您使用的数据库.
为避免混淆,使用不同的列名称显然会更好.
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |