データベースの列名が「from」ですが、列名を他の名前に変更しているときにエラーが発生します。エラーは発生しません。なぜですか?
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();
}
列名を都市に変更してもエラーが発生しないのはなぜですか?
from
SQLで予約されているキーワードです。テーブル名として使用する場合は、次の方法でエスケープする必要があります。
だからどちらか
insert into Mynew(name,age,`from`,address,job)values(@name,@age,@from,@address,@job)
または
insert into Mynew(name,age,[from],address,job)values(@name,@age,@from,@address,@job)
使用しているデータベースによって異なります。
混乱を避けるために、別の列名を使用することをお勧めします。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加