Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more: , +
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)";



Thanks in advance
Posted
Updated 27-Mar-14 22:04pm
v4
Comments
Tejas Vaishnav 27-Mar-14 7:34am    
what have your tried so far to save one record... please use improve question and add you code blocks..
ZurdoDev 27-Mar-14 10:48am    
Call INSERT INTO 3 times, or UPDATE 3 times. Where are you stuck?
agent_kruger 27-Mar-14 15:59pm    
no sir, on one connection i want to save as many record as i want indirectly it will save the time to make connections again and again.
ZurdoDev 27-Mar-14 16:04pm    
Yes. You can do as many insert statements as you want in a single connection. Otherwise, what's your plan?
agent_kruger 28-Mar-14 4:00am    
sir, my databse is ACCESS can i do it now?

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.
 
Share this answer
 
Comments
agent_kruger 28-Mar-14 8:47am    
no sir, it wont make a BIG difference as the request is send again and again to the database.
ZurdoDev 28-Mar-14 8:49am    
You're right. The request is sent again and again but the connection is left open. That's the best you can do. Access does not support batch statements.

Especially if you are talking about only 3 statements I doubt you would even be able to notice the difference anyway.
agent_kruger 28-Mar-14 8:52am    
ok sir.
agent_kruger 28-Mar-14 8:49am    
and sir, i also know that unlike sql it cannot execute many statement in a single go but thought that i would get some alternate for this here
see here sample example which i used in my project works well.

change your column values whichever you want

C#
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");
 
Share this answer
 
Comments
agent_kruger 29-Mar-14 17:22pm    
sir, this is just to insert one record at a time please read the question again.
There are a couple of ways to do this: the simplest is:
INSERT INTO MyTable (ColumnOne, ColumnTwo)
VALUES (1,2), (3,4), (5,6)
 
Share this answer
 
Comments
agent_kruger 28-Mar-14 4:03am    
sir, my database is MS ACCESS not SQL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900