将数据从C#插入Access [英] Inserting datas from C# to Access

查看:73
本文介绍了将数据从C#插入Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何将数据从C#插入Access。我正在使用访问权限2007.

我尝试过以下代码但是它不起作用,

Hi,
How to insert a data from C# to Access.I''m using access 2007.
I have tried the following codebut it is not working,

con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.12.0;Data Source=D:\\weightbridge.mdb;Persist Security Info=False");
            con.Open();
            cmd = new OleDbCommand("Insert into SupplierMaster Values('"+comboBox1.SelectedItem+"','"+textBox1.Text+"')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Request Sent Successfully");



问候

Balamurugan


Regards
Balamurugan

推荐答案

这段代码太可怕了。阅读SQL注入,了解如何使用表单擦除数据库。无论哪种方式,如果您希望我们帮助您,您需要告诉我们错误消息。假设表中只有两个可插入的字段,您的代码看起来很好。我将添加SQL以列出要插入的列,然后如果这没有帮助,请阅读错误消息(我们将告诉您它意味着它的内容)并在此处发布它如果您仍然没有得到它(编辑你的帖子)
This code is horrible. Read up on SQL Injection to see how I can use your form to erase your database. Either way, you need to tell us the error message if you want us to help you. Your code looks fine, assuming the table only has two fields that can be inserted. I''d add the SQL to list what columns you want to insert in to, and then if that does not help, read the error message ( we are going to tell you that it means what it says ) and post it here if you still don''t get it ( edit your post )


if(dr!= null && dr.HasRows)

{

//dbReader.Read()将返回true,直到没有更多行要读取

while(dr.Read())

{

listBox1.Items.Add(dr [SupCode]);

}

}
if (dr != null && dr.HasRows)
{
//dbReader.Read() will return true until there are no more rows to be read
while (dr.Read())
{
listBox1.Items.Add(dr["SupCode"]);
}
}


private void button5_Click(对象发送者,EventArgs e)

{

string nam;

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

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(INSERT INTO SupplierMaster VALUES (''+ textBox2.Text +'',''+ textBox1.Text +''),con);

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show(SupplierMaster成功保存);



}
private void button5_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
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("INSERT INTO SupplierMaster VALUES(''" + textBox2.Text + "'',''" + textBox1.Text + "'')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Saved Successfully");

}


这篇关于将数据从C#插入Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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