在c#win中访问一个连接中保存多个记录。形成 [英] Save multiple records in one connection in access in c# win. form

查看:59
本文介绍了在c#win中访问一个连接中保存多个记录。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#中的access数据库在一个连接中保存多个记录。喜欢只通过一个连接保存3条记录。



在一个连接上保存1条记录

How to save multiple records in one connection by using access database in c#. Like saving 3 records by making only one connection.

For saving 1 record at one connection

string Data = "Insert Into Tbl(Field1,Field2)values(0,1)";







谢谢advance




Thanks in advance

推荐答案

我认为Access不支持批处理语句,这意味着INSERT ......后跟另一个INSERT ...所以,你需要做的就是打开你的连接然后一个接一个地提交sql语句,或者根据你拥有的内容,你可以在循环中完成它们。然后当你完成后你关闭连接。
I don't think Access supports batch statements, meaning INSERT ... followed by another INSERT ... So, all you need to do is open your connection and then submit the sql statements one after the other or depending on what you have you can do them in a loop. Then when you are done you close the connection.


看这里我在我的项目中使用的示例示例效果很好。



根据需要更改列值



see here sample example which i used in my project works well.

change your column values whichever you want

cmdd.CommandText = "INSERT into addcontact([addusername],[addimage],[addimagename],[addname],[addbirthday],[addgender],[addrelation],[addemail],[addnumber1],[addnumber2],[addnumber3],[addnumber4],[addaddress1]) VALUES(@addusername,@addimage,@addimagename,@addname,@addbirthday,@addgender,@addrelation,@addemail,@addnumber1,@addnumber2,@addnumber3,@addnumber4,@addaddress1)";

cmdd.Connection = conn;

            cmdd.Parameters.AddWithValue("@addusername", loginusername);
            cmdd.Parameters.AddWithValue("@addimage", imgByteArr);
            cmdd.Parameters.AddWithValue("@addimagename", te);
            cmdd.Parameters.AddWithValue("@addname", txtnamecontact.Text);
            cmdd.Parameters.AddWithValue("@addbirthday", datt);
            cmdd.Parameters.AddWithValue("@addgender", rad);
            cmdd.Parameters.AddWithValue("@addrelation", comboBox1.Text);
            cmdd.Parameters.AddWithValue("@addemail", txtemail.Text);
            cmdd.Parameters.AddWithValue("@addnumber1", txtnum1.Text);
            cmdd.Parameters.AddWithValue("@addnumber2", txtnum2.Text);
            cmdd.Parameters.AddWithValue("@addnumber3", txtnum3.Text);
            cmdd.Parameters.AddWithValue("@addnumber4", txtnum4.Text);
            cmdd.Parameters.AddWithValue("@addaddress1", txtadd1.Text);
            conn.Open();
            cmdd.ExecuteNonQuery();
            conn.Close();            
            System.Windows.Forms.MessageBox.Show("inserted sucessfully", "Success");


有有两种方法可以做到这一点:最简单的是:

There are a couple of ways to do this: the simplest is:
INSERT INTO MyTable (ColumnOne, ColumnTwo)
VALUES (1,2), (3,4), (5,6)


这篇关于在c#win中访问一个连接中保存多个记录。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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