2

これは私のコードです。これらの値をデータベースに保存したいのですが、エラーが発生しました。

キーワード値付近の不適切な構文

foreach (GridViewRow gvr in GridView1.Rows)
{
string strcon1;
strcon1 = ConfigurationManager.ConnectionStrings["fwma_devConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(strcon1);
con1.Open();
SqlCommand com3 = new SqlCommand(strcon);
TextBox tb = (TextBox)gvr.FindControl("TextBox2");//value
string txt = tb.Text;

Label propertylabel = (Label)gvr.FindControl("Label4");//id-property

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";
com3.Connection = con1;
com3.ExecuteNonQuery();
con1.Close();
4

3 に答える 3

3

これを使って

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values('" + propertylabel.Text + "','" + B_id.Text + "','" + tb.Text + "')";

それ以外の

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";
于 2013-05-27T05:00:22.807 に答える
1

この線はこのようであるべきではありませんか?

        com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + ")";

コマンドパラメータを使用してください:

SqlCommand パラメータを追加するときに、いつ「SqlDbType」と「size」を使用する必要がありますか?

于 2013-05-27T05:01:09.190 に答える
1

予約済みのキーワードを使用している場合は、引用符または大括弧で区切られた識別子を指定する必要があります。

括弧付きの使用例

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,[Values]) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";
于 2013-05-27T05:11:19.333 に答える