更新和删除Access到C#.net的编码 [英] Updating and Deleting coding from Access to C#.net

查看:67
本文介绍了更新和删除Access到C#.net的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何编写C#中的更新和删除按钮的编码。对于数据库我正在使用Access 2007.我已经尝试了代码,但它没有工作显示错误下面我用过的代码,

string nam;

WeightBridge.Class1 obj = new WeightBridge.Class1();

nam = obj。 get();

OleDbConnection con = new OleDbConnection(nam);

con.Open();

OleDbCommand cmd = new OleDbCommand(" ;更新SupplierMaster SET [SupplierCode] =''" + textBox2.Text +"'',[SupplierName] =''" + textBox1.Text +"'',[SupplierCode] =''" + ID +"''",con);

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show (SupplierMaster已成功更新);

Hi,
How to write the coding for Update and Delete button in C#.For data base i''m using Access 2007.I have tried the code but it s not working showing error the below code which i have used,
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("Update SupplierMaster SET [SupplierCode]=''" +textBox2.Text + "'',[SupplierName]=''" +textBox1.Text + "'',[SupplierCode]=''" + ID + "'' ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Updated Successfully");

推荐答案

在您提供的SQL命令中,SupplierCode字段被指定两次。



可能您忘记指定 WHERE 子句,也许你也错误输入了最后一个字段名。



问候,

Daniele。
Within the SQL command you provided, SupplierCode field is specified twice.

Probably You have forgot to specify the WHERE clause, and maybe you also mistyped the last field name.

Regards,
Daniele.

对于从访问C#更新,我们应该遵循以下代码,

private void button1_Click(object sender,EventArgs e)

{

string nam;

WeightBridge.Class1 obj = new WeightBridge.Class1();

nam = obj.get();

nam =Provider = Microsoft.ACE.OLEDB.12.0; Data Source = d:\\ weightbridge.accdb; Persist Security Info = False;;

OleDbConnection con = new OleDbConnection(nam);

con.Open();

OleDbCommand cmd = new OleDbCommand(更新SupplierMaster SET [ SupplierCode] =+ textBox2.Text +,[SupplierName] =''+ textBox1.Text +''WHERE [SupplierCode] =+ ID +,con);

cmd .ExecuteNonQuery();

con.Close();

MessageBox.Show(SupplierMaster已成功更新);

this.SupplierMaster_Load( new object(),new EventArgs());

}

对于从访问C#的删除,我们应该遵循以下代码,

private void button2_Click(object sender,EventArgs e)

{

string nam;

WeightBridge.Class1 obj = new WeightBridge.Class1();

nam = obj.get();

nam =Provider = Microsoft.ACE.OLEDB.12.0;数据源= d:\\\\weightbridge.accdb ; Persist Security Info = False;;

OleDbConnection con = new OleDbConnection(nam);

con.Open();

OleDbCommand cmd = new OleDbCommand(从SupplierMaster中删除[SupplierCode] =+ ID +,con);

cmd.ExecuteNonQuery();

con.Close() ;

MessageBox.Show(已成功删除);

this.SupplierMaster_Load(new object(),new EventArgs());



}
For Updating from access to C# we should follow the below code,
private void button1_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
nam = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\weightbridge.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("Update SupplierMaster SET [SupplierCode]=" +textBox2.Text + ",[SupplierName]=''" +textBox1.Text + "'' WHERE [SupplierCode]=" + ID + " ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Updated Successfully");
this.SupplierMaster_Load(new object(),new EventArgs());
}
For Deleing from access to C# we should follow the below code,
private void button2_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
nam = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\weightbridge.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("Delete from SupplierMaster where [SupplierCode]=" + ID + " ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted Successfully");
this.SupplierMaster_Load(new object(), new EventArgs());

}


这篇关于更新和删除Access到C#.net的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆