PDA

View Full Version : راهنمايي براي دستور Update



hallaji
سه شنبه 19 شهریور 1387, 17:04 عصر
با سلام خدمت دوستان عزيز و برنامه نويسان گرامي
بنده براي update كردن جدول T كه شامل فيلد zaman و row ‌است از اين كد استفاده ميكنم
چون با توجه به :
http://barnamenevis.org/forum/showthread.php?p=590591
بايد واسه هر row‌ يك دستور update مينوشتم


try
{
SqlConnection con = new SqlConnection();
SqlCommand cmd1 = new SqlCommand();
SqlCommand cmd2 = new SqlCommand();
SqlCommand cmd3 = new SqlCommand();
SqlCommand cmd4 = new SqlCommand();
SqlCommand cmd5 = new SqlCommand();
SqlCommand cmd6 = new SqlCommand();
SqlCommand cmd7 = new SqlCommand();
SqlCommand cmd8 = new SqlCommand();
SqlCommand cmd9 = new SqlCommand();
SqlCommand cmd10 = new SqlCommand();
SqlCommand cmd11 = new SqlCommand();
SqlCommand cmd12 = new SqlCommand();
con.ConnectionString = "server=(local);database=program;uid=mehdi;password =123456";
cmd1.CommandText = "update t set zaman='" + Txt1.Text + "' where row='1'";
cmd2.CommandText = "update t set zaman='" + Txt2.Text + "' where row='2'";
cmd3.CommandText = "update t set zaman='" + Txt3.Text + "' where row='3'";
cmd4.CommandText = "update t set zaman='" + Txt4.Text + "' where row='4'";
cmd5.CommandText = "update t set zaman='" + Txt5.Text + "' where row='5'";
cmd6.CommandText = "update t set zaman='" + Txt6.Text + "' where row='6'";
cmd7.CommandText = "update t set zaman='" + Txt7.Text + "' where row='7'";
cmd8.CommandText = "update t set zaman='" + Txt8.Text + "' where row='8'";
cmd9.CommandText = "update t set zaman='" + Txt9.Text + "' where row='9'";
cmd10.CommandText = "update t set zaman='" + Txt10.Text + "' where row='10'";
cmd11.CommandText = "update t set zaman='" + Txt11.Text + "' where row='11'";
cmd12.CommandText = "update t set zaman='" + Txt12.Text + "' where row='12'";
cmd1.Connection = con;
cmd2.Connection = con;
cmd3.Connection = con;
cmd4.Connection = con;
cmd5.Connection = con;
cmd6.Connection = con;
cmd7.Connection = con;
cmd8.Connection = con;
cmd9.Connection = con;
cmd10.Connection = con;
cmd11.Connection = con;
cmd12.Connection = con;
con.Open();
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
cmd6.ExecuteNonQuery();
cmd7.ExecuteNonQuery();
cmd8.ExecuteNonQuery();
cmd9.ExecuteNonQuery();
cmd10.ExecuteNonQuery();
cmd11.ExecuteNonQuery();
cmd12.ExecuteNonQuery();
con.Close();
datagrid();
}
catch
{
MessageBox.Show("امكان ويرايش وجود ندارد", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}

حالا از دوستان راهنمايي ميخواستم تا ببينم آيا روش بهتري براي update كردن وجود دارد؟

saber4166
سه شنبه 19 شهریور 1387, 18:26 عصر
شما چقدر شی تعریف کردین شما میتونستید از یه حلقه ی while استفاده کنید و یه شی از نوع SQLCOMMAND و executenone.. و ........ رو فقط یک بار بسازی
دقت داشته باش در مقدار دهی دوباره commend هات و در where خودت هم دقت لازم رو داشته باش تا یک دستور رو 2بار اجرا نکنه