PDA

View Full Version : سوال: ارتباط سی شارپ ودیتابیس



hileila
یک شنبه 29 بهمن 1391, 09:32 صبح
سلام من یه برنامه تو سی شارپ مینویسم که از دیتابیس اکسس استفاده میکنم برای درج اطلاعات از دستورinsertاستفاده کردم که موقع اجرا به دستور
cmd.ExecuteNonQueryایراد میگیره،در اکسس برای insertاز دستور دیگه ای باید استفاده کنم؟؟؟؟؟؟؟؟؟؟

rezaei_y
یک شنبه 29 بهمن 1391, 09:41 صبح
نه دوست عزیز همون insert

کدتو بذار ببینیم

hileila
یک شنبه 29 بهمن 1391, 12:48 عصر
یه کلاس تعریف کردم به نامminorclass که دستورات زیرو درونش قرار دادم
mydb db = new mydb

public void Add(string code,string fname,string family,string voroud,string khoroj,string dated,string pedar,string meli,string taahol,string vaziatkari,string vaziatestekhdami,string tavalod,string mobile,string tel1,string tel2,string address,string tozihat)
{
string acc = " insert into personely(code,fname,family,voroud,khoroj,dated,pe dar,meli,taahol,vaziatkari,vaziatestekhdami,tavalo d,mobile,tel1,tel2,address,tozihat)Values('{0}','{ 1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9} ','{10}','{11}','{12}','{13}','{14}','{15}' ,'{16}')";
acc = string.Format(acc,code, fname, family, voroud, khoroj, dated, pedar, meli, taahol, vaziatkari, vaziatestekhdami, tavalod, mobile, tel1, tel2, address, tozihat);
db.connect();
db.docommand(acc);
db.connect();
برای دکمه ثبت اطلاعاتم کدهای زیرو قرار دادم
minorclass mc = new minorclass


mc.Add(txtcode.Text, txtname.Text, txtfamily.Text, txtsaatv.Text, txtsaatkho.Text, txttarikhestekh.Text, txtpedar.Text, txtmeli.Text, txttaahol.Text, txtvazkar.Text, txtvazestekh.Text, txttavalod.Text, txtmob.Text, txttel1.Text, txttel2.Text, txtaddress.Text, txttozih.Text)

vapa_71
یک شنبه 29 بهمن 1391, 13:52 عصر
من این کد ها رو دارم

ثبت

OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=dblog.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4231355");
OleDbCommand com = new OleDbCommand();
com.CommandText = "insert into tblog (usern, [password]) values ('" + textBox1.Text + "','" + textBox2.Text + "')";
cn.Open();
com.Connection = cn;
com.ExecuteNonQuery();
cn.Close();
MessageBox.Show("کاربر جدید ثبت گردید", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Close();

ویرایش

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=dblog.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4231355");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM tblog WHERE usern=@use", con);
da.SelectCommand.Parameters.AddWithValue("@use ", textBox4.Text);
DataTable login = new DataTable();
da.Fill(login);
if (login.Rows.Count > 0)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.Parameters.AddWithValue("@user", textBox4.Text);
cmd.Parameters.AddWithValue("@pass", textBox3.Text);
cmd.CommandText = "UPDATE tblog SET usern = @user, [password] = @pass WHERE usern = @user;";
con.Open(); // open the connection
int numAffected = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("رمز عبور تغییر یافت", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Close();
}
else
{
MessageBox.Show("اطلاعات وارد شده اشتباه است", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

حذف

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=dblog.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4231355");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM tblog WHERE usern=@use", con);
da.SelectCommand.Parameters.AddWithValue("@use ", textBox5.Text);
DataTable login = new DataTable();
da.Fill(login);
if (login.Rows.Count > 0)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.Parameters.AddWithValue("@user", textBox5.Text);
cmd.CommandText = "DELETE FROM tblog WHERE usern = @user;";
con.Open(); // open the connection
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("کاربر حذف گردید", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Close();
}
else
{
MessageBox.Show("اطلاعات وارد شده اشتباه است", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}