PDA

View Full Version : آپدیت رکورد



armina_maleki_89
جمعه 08 خرداد 1388, 12:43 عصر
با سلام ، دستور پایین رو که اجرا میکنم پیغام خطای :
Syntax error in update statement میده ، ایرادش چیه؟
(پایگاه دادمو با اکسس درست کردم)
ممنون می شم اگه زود جوابمو بدین ، آخه فردا تحویل پروژه دارم
private void button3_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source D:\\anbar.mdb ;";

OleDbCommand command1 = con.CreateCommand();
command1.CommandText = "update ghete set ( radif ='" + textBox1.Text + "',name_ghete ='" + textBox2.Text + "',code_ghete ='" + textBox3.Text + "', tedad_darkhasti ='" + textBox4.Text + "', tedad_mojod='" + textBox5.Text + "', taminkonande='" + textBox6.Text + "',sal='" + comboBox1.Text + "',mah='" + comboBox2.Text + "',rooz='" + comboBox3.Text + "')";
con.Open();
command1.ExecuteNonQuery();
con.Close();
command1.Dispose();
}

Reza_Yarahmadi
جمعه 08 خرداد 1388, 14:50 عصر
Data Source = D:\\anbar.mdb ;"


مشکل در دستور Update ، فکر کنم اگه پرانتزها رو برداری درست بشه در ضمن هیچ شرطی واسه این دستور نذاشتی اگه این دستور انجام بشه تمام رکوردهات ویرایش میشن.
یه چیز دیگه نیازس به Dispose کردن Command نیست خود #C این کارو انجام میده
امیدوارم مشکلت حل بشه

mahdi_7610
جمعه 08 خرداد 1388, 17:40 عصر
برای اپدیت باید از WHERE استفاده کنی .

موفق باشی .

h_r_sh
شنبه 09 خرداد 1388, 23:43 عصر
command1.CommandText = "update ghete set [radif] ='" + textBox1.Text + "',[name_ghete] ='" + textBox2.Text + "',[code_ghete] ='" + textBox3.Text + "', [tedad_darkhasti] ='" + textBox4.Text + "', [tedad_mojod]='" + textBox5.Text + "', [taminkonande]='" + textBox6.Text + "',[sal]='" + comboBox1.Text + "',[mah]='" + comboBox2.Text + "',[rooz]='" + comboBox3.Text + "')";


it's highly recommended that use parameter(s) as shown below:



oledbconnection cn=new oledbconnection(myConnectionString);
string command text="UPDATE myTable set [field1]=@a,[field2]=@b where ([field3]=@c);
oledbcommand cmd=new oledbcommand(commandText,cn);
oledbparameter ap=new oledbparameter("@a",oledbtype.varchar);
ap.value="myValue1";
oledbparameter bp=new oledbparameter("@b",oledbtype.varchar);
ap.value="myValue2";
oledbparameter cp=new oledbparameter("@c",oledbtype.varchar);
ap.value="myValue3";
//other way:
//oledbparameter ap=new oledbparameter("@a","myValue1");
//oledbparameter bp=new oledbparameter("@b","myValue2");
//oledbparameter cp=new oledbparameter("@c","myValue3");
cmd.parameters.addrange(new oledbparameter[] {ap,bp,cp});
if(cn.state==connectionstate.closed) cn.open();
cmd.executenonquery();
cn.close()