PDA

View Full Version : سوال: انتقال یک کد به دیتابیس



mehrshadms
یک شنبه 23 تیر 1392, 10:08 صبح
سلام من کد درج د دیتابیس رو نوشتم به صورت زیر

SqlConnection con1 = new SqlConnection();
con1.ConnectionString = global::WIN1.Properties.Settings.Default.connectio nstrring;
con1.Open();
SqlCommand c1 = new SqlCommand();
c1.Connection = con1;
c1.CommandText = "insert into TBLCASABIT(CARI_KOD , ULKE_KODU , CARI_ISIM , CARI_TEL , EMAIL , POSTAKODU , GRUP_KODU , RAPOR_KODU4 , RAPOR_KODU1 , RAPOR_KODU2 , PLASIYER_KODU , ODEMETIPI , SUBE_KODU , ISLETME_KODU )values(N'214" + txtCustomerCode.Text + "', N'" + txtPass.Text + "', N'" + txtFname.Text + " " + "' , N'" + txtMobNumber.Text + "' , N'" + txtEmail.Text + "', N'" + txtPCode.Text + "', N'" + cmb2.SelectedIndex + "', N'" + txtMasir.Text + "' , N'" + txtbakhsh.Text + "', N'" + txtSenf.Text + "', N'" + txtVisitor.Text + "', N'" + comboPayment.SelectedIndex + "' , N'" + Convert.ToInt32(lblSubeKodu.Text) + "', N'" + Convert.ToInt32(lblheading.Text) + "') ";
c1.ExecuteNonQuery();
con1.Close();

می خوام این کد رو ببرم تو کلاس و در همه جا ازش استفده کنم کسی می تونه راهنماییم کنه ( من تو این مورد چیزی نمی دونم لطفاً امل راهنمای کنید)
ممنون

hamid_hr
یک شنبه 23 تیر 1392, 10:20 صبح
خيلي سادش به اين صورت ميشه
public void Insert(string[] s){
SqlConnection con1 = new SqlConnection();

con1.ConnectionString = global::WIN1.Properties.Settings.Default.connectio nstrring;

con1.Open();
SqlCommand c1 = new SqlCommand();
c1.Connection = con1;
c1.CommandText = "insert into TBLCASABIT(CARI_KOD , ULKE_KODU , CARI_ISIM , CARI_TEL , EMAIL , POSTAKODU , GRUP_KODU , RAPOR_KODU4 , RAPOR_KODU1 , RAPOR_KODU2 , PLASIYER_KODU , ODEMETIPI , SUBE_KODU , ISLETME_KODU )values(N'214" + s[0] + "', N'" + s[1] + "', N'" + s[2] + " " + "' , N'" + s[3] + "' , N'" + s[4] + "', N'" + s[5] + "', N'" + s[6] + "', N'" + s[7] + "' , N'" + s[8] + "', N'" + s[9] + "', N'" + s[10] + "', N'" + s[11] + "' , N'" + s[12] + "', N'" + Convert.ToInt32(s[13]) + "') ";
c1.ExecuteNonQuery();
con1.Close();
}
تو اين حالت تمام متغير هاتو به صورته يك ارايه اي از string در مياري و پاس ميدي اينجا

اگه يكم منظم تر بخواي بايد يه struct درست كني كه تمام اعضا جدولتو داشته باشه بعد اونو به اين تابع پاس بدي

mehrshadms
یک شنبه 23 تیر 1392, 10:37 صبح
struct درست کردم این حله ولی یه سوال دیگه تو کد زیر select زدم حالا تو فرم اصلی رو باتن که کلیک شد می خوام مقادیر بره تو گرید ویو اینم راهنماییم کن ممنون

SqlConnection con1 = new SqlConnection();
con1.ConnectionString = global::WIN1.Properties.Settings.Default.connectio nstrring;
con1.Open();
SqlCommand c1 = new SqlCommand();
c1.Connection = con1;
c1.CommandText = "select CARI_KOD as [کد مشتری] , CARI_ISIM as [نام مشتری] from TBLCASABIT where CARI_KOD like '214%'";
c1.ExecuteNonQuery();
con1.Close();

hamid_hr
یک شنبه 23 تیر 1392, 10:52 صبح
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = global::WIN1.Properties.Settings.Default.connectio nstrring;
con1.Open();
SqlCommand c1 = new SqlCommand();
c1.Connection = con1;
c1.CommandText = "select CARI_KOD as [کد مشتری] , CARI_ISIM as [نام مشتری] from TBLCASABIT where CARI_KOD like '214%'";

SqlDataReader sdr = c1.ExecuteReader (System.Data.CommandBehavior.CloseConnection);
dgv1.rows.clear();
while(sdr.read())
{
dgv1.rows.add(sdr[0],sdr[1]);
}
sdr.close();

mehrshadms
یک شنبه 23 تیر 1392, 10:56 صبح
کد مربوط به select تو کلاس نوشته شده من می خوام تو فرن اصلی فراخونی کنم ، 2 تاشون پیش هم نیسن

hamid_hr
یک شنبه 23 تیر 1392, 11:01 صبح
public arraylist Select(){
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = global::WIN1.Properties.Settings.Default.connectio nstrring;
con1.Open();
SqlCommand c1 = new SqlCommand();
c1.Connection = con1;
c1.CommandText = "select CARI_KOD as [کد مشتری] , CARI_ISIM as [نام مشتری] from TBLCASABIT where CARI_KOD like '214%'";

SqlDataReader sdr = c1.ExecuteReader (System.Data.CommandBehavior.CloseConnection);
dgv1.rows.clear();
arraylist arl =new arraylist();
object[] tmp;
while(sdr.read())
{
tmp=new object[2];
tmp[0]=sdr[0];
tmp[1]=sdr[1];
arl.add(tmp);
}
sdr.close();
return arl;
}

اينم زير فرم بنويس

arraylisr arl=select();
foreach(object[] tmp in arl)
{
dgv1.rows.add(tmp[0],tmp[1]);
}