PDA

View Full Version : مبتدی: ویرایش و حذف از DataGridView و اعمال در بانک



nimamohammadi
شنبه 08 اسفند 1388, 19:44 عصر
سلام
من یه دیتا گرید ویو دارم که می خوام تغییرات رو در همون دیتا گرید ویو اعمال کنم و وقتی دکمه ویرایش رو زدم ، ویرایش بشه
و می خوام وقتی یک سطر رو انتخاب کردم و دکمه حذف رو زدم از پایگاه داده (sql 2008) حذف بشه.
خیلی سرچ کردم ولی نتیجه ای نگرفتم.
لطفا کمک، خیلی فوری نیاز دازم.
لطفا اگه نمونه و مثالی دارید . برام بذارید
واقعا ممنون میشم.

csharpprogramer88
شنبه 08 اسفند 1388, 20:11 عصر
سلام

ويرايش


if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.Connection = con;
cmd.CommandText = "update moraselat set id='" + txtid.Text + "',subject='" + txtsubject.Text + "',date='" + txtdate.Text + "',shomare='" + txtshomare.Text + "',saheb='" + txtsaheb.Text + "',vazeiat='" + cmbvazeiat.Text + "',peigiri='" + txtpeigiri.Text + "',zaman='" + txtzaman.Text + "',tozihat='" + txttozihat.Text + "',eghdam='" + txteghdam.Text + "',girande='" + txtgirande.Text + "' where id=" + dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();
cmd.ExecuteReader();
con.Close();
dataGridViewX1.Refresh();//نشان دادن فوري پس از آپ ديت
con.Close();


حذف



string str1 = "delete from moraselat where id=" + dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[0].Value.ToString();
cmd.Connection = con;
cmd.CommandText = str1;
cmd.ExecuteNonQuery();
con.Close();
dataGridViewX1.Refresh();
dataGridViewX1.Rows.Remove(dataGridViewX1.CurrentR ow);

خط قرمز براي اينكه بعد از عمل حذف فيزيكي از ديتابيس هم حذف بشه
بازم مشكلي بود سريع بگو

nimamohammadi
شنبه 08 اسفند 1388, 21:07 عصر
#region "Delete"
DataBase db=new DataBase();
db.DoCommand("delete from telephon where name='" + txtName.Text + "' and family='" + txtFamily.Text + "'");
//'update datagrid
Form1_Load(sender, e);

MessageBox.Show("Deleted");
#endregion
این کد برای دیتا بیس اکسسه . به جای docommand چه چیزی میشه گذاشت ؟ (برای sql2008)
به جای database باید از dataset استفاده کنم؟ درسته؟

csharpprogramer88
شنبه 08 اسفند 1388, 21:17 عصر
شما با اكسس كار مي كني يا با اس كيو ال من زياد اكسس نمي دونم

nimamohammadi
شنبه 08 اسفند 1388, 21:25 عصر
با اس کیو ال کار می کنم.
منظورتون مثلا از txtid.text چیه ؟
مگه داخل همون دیتا گرید ویو تغییر رو ایجاد نمی کنید؟
منظورتون فیلد های دیتا گرید ویو ست؟

nimamohammadi
شنبه 08 اسفند 1388, 21:30 عصر
من جستجو رو در یک فرم جداگانه انجام میدم .
یعنی هیچ تکستی به جز تکستی که جستجو بر اساس اون انجام میشه ندارم. وبعد فقط دیتا گرید ویو دارم و دکمه هامو.

csharpprogramer88
شنبه 08 اسفند 1388, 21:40 عصر
id='" + txtid.Text + "'
براي آپ ديت
دوست عزيز id فيلدت توي ديتابيسته '" + txtid.Text + "' پارامتري است كه بايد آپ ديت شود

براي حذف :
id كليد اصلي در جدول است كه يكتاست و دستورات بعد از Idبراي حذف ركورد فعال است اگر آي دي را وارد نكرديد ممكن است كل اطلاعات حذف شود

ali_autumnal
یک شنبه 09 اسفند 1388, 00:05 صبح
سلام
اینا نگاه کن که کاملا چک شده و درست هستن.


Insert Code

SqlConnection connect_data = new SqlConnection("data source=(local);initial catalog=Cheak_Data;integrated security=true;");
connect_data.Open();
SqlCommand usercommand = new SqlCommand();
usercommand.Connection = connect_data;
usercommand.CommandText = "insert into tbluser(radif,fname,lname,codemeli,password,nooeus er,vazeiat,pedar,idshenasname,tarikhetavalod,mahal etavalod,tel,mob,address,pic)values ('" + "" + "','" + txtfname.Text + "','" + txtlname.Text + "','" + mtmeli.Text + "','" + txtpassword.Text + "','" + cmbnooeuser.Text + "','" + cmbvazeiat.Text + "','" + txtpedar.Text + "','" + txtidshenasname.Text + "','" + mttavalod.Text + "','" + txtmahaletavalod.Text + "','" + txttel.Text + "','" + txtmob.Text + "','" + txtaddress.Text + "','" + txtmasirepic.Text + "')";
usercommand.ExecuteNonQuery();


Edit Or Delete

SqlConnection connect_data = new SqlConnection("data source=(local);initial catalog=Cheak_Data;integrated security=true;");
connect_data.Open();
SqlCommand usercommand = new SqlCommand();
usercommand.Connection = connect_data;

usercommand.CommandText = "update tbluser set radif='" + "" + "',fname='" + txtfname.Text + "',lname='" + txtlname.Text + "',codemeli='" + mtmeli.Text + "',password='" + txtpassword.Text + "',nooeuser='" + cmbnooeuser.Text + "',vazeiat='" + cmbvazeiat.Text + "',pedar='" + txtpedar.Text + "',idshenasname='" + txtidshenasname.Text + "',tarikhetavalod='" + mttavalod.Text + "',mahaletavalod='" + txtmahaletavalod.Text + "',tel='" + txttel.Text + "',mob='" + txtmob.Text + "',address='" + txtaddress.Text + "',pic='" + txtmasirepic.Text + "' where codemeli='" + dglistper.SelectedRows[0].Cells[3].Value.ToString() + "'";
usercommand.ExecuteNonQuery();
procerror("پيام ثبت ويرايش", "اطلاعات کاربر فوق با موفقيت ويرايش شد");
////////////////================================================== =====
usercommand.CommandText = "update tblbank set codeuser='" + mtmeli.Text + "' where codeuser='" + dglistper.SelectedRows[0].Cells[3].Value.ToString() + "'";
usercommand.ExecuteNonQuery();
usercommand.CommandText = "update tblidhesab set codeuser='" + mtmeli.Text + "' where codeuser='" + dglistper.SelectedRows[0].Cells[3].Value.ToString() + "'";
usercommand.ExecuteNonQuery();
usercommand.CommandText = "update tblsodorecheak set codeuser='" + mtmeli.Text + "' where codeuser='" + dglistper.SelectedRows[0].Cells[3].Value.ToString() + "'";
usercommand.ExecuteNonQuery();
usercommand.CommandText = "update tbltarifecheak set codeuser='" + mtmeli.Text + "' where codeuser='" + dglistper.SelectedRows[0].Cells[3].Value.ToString() + "'";
usercommand.ExecuteNonQuery();
////////////////================================================== =====

Sajjad.Aghapour
یک شنبه 09 اسفند 1388, 00:43 صبح
دوست عزیز بارها این مسئله مطرح شده و جواب هم داده شده...
برای این کار از SqlCommandBuilder استفاده کنید....



SqlCommandBuilder b = new SqlCommandBuilder(objDataAdapter);
objDataAdapter.Update(objDataTable);


دیگه نیازی هم نیست که برای تک تک تغییرات که اعمال کردید یک کوئری بنویسید.این کد همه تغییرات رو تشخیص میده و بطور یکجا در دیتابیس اعمال می کنه...
موفق باشید