Click here to Skip to main content
14,664,481 members
Rate this:
Please Sign up or sign in to vote.
I'm trying to make a project that has a database(Microsoft Access) I can read what's on the database using Windows forms application but I really can't understand how to save data in the database, Can anyone give me a sample project regarding OleDB? I really can't find a good guide in the google :(

here is my code for reading data in the database, and I plan to make a register form, that saves data in the textbox :) thanks!

OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb");

       private void Button1_Click(object sender, EventArgs e)
       {
           cn.Open();
           OleDbDataReader oreader = null;
           OleDbCommand commando = new OleDbCommand("select* from Accountstbl", cn);
           oreader = commando.ExecuteReader();
           while (oreader.Read())
           {
               user.Add(oreader["Username"].ToString());
               pass.Add(oreader["Password"].ToString());
               rights.Add(oreader["Rights"].ToString());
           }
           cn.Close();
           int totalItems = user.Count;
           int count = 0;
           string isValidated = "";
           while (count < totalItems)
           {
               if ((textBox1.Text == user[count].ToString()) && (textBox2.Text == pass[count].ToString()))
               {
                   isValidated = "True";
                   rights2 = rights[count].ToString();
                   count = 100;
               }
               else
               {
                   count++;
               }
           }
           if (isValidated == "True")
           {
               this.Hide();
               Form2 posForm = new Form2();
               posForm.ShowDialog();
               textBox1.Clear();
               textBox2.Clear();

           }
           else
           {
               MessageBox.Show("Username and Password do not match! Please try again!", "ERROR",MessageBoxButtons.OK ,MessageBoxIcon.Error);
           }
       }
Posted
Comments
alfred sanz 4-Feb-13 20:24pm
   
PLease . .. . :(
Jibesh 4-Feb-13 20:32pm
   
you can use Insert commands to insert your data back to your access database. you may found as many articles in Code Project or simple google.

like the the way select command you used, use insert Into - to insert a new values or update - command to update the existing data in your data base
alfred sanz 4-Feb-13 20:55pm
   
Thanks :) I'll try it
Jibesh 4-Feb-13 21:00pm
   
I have updated my solution have a look at it. use proper command to save your data to the database
alfred sanz 4-Feb-13 21:20pm
   
thanks :)
Mike Meinz 4-Feb-13 20:33pm
   
See Insert and Update statement documentation.

Rate this:
Please Sign up or sign in to vote.

Solution 2

Use the Insert statement like this. This is just a sample replace the Table and field names with your actual table and field names

OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\sisc-erelim\4_Printing\VTDB\DB\VirginiTEADB2.accdb");
 
string myExecuteQuery = "INSERT INTO TABLE_NAME (FIELD1,FIELD2) VALUES (VALUE1,VALUE2)";
   OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, cn);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();


If you want to update the existing value, use UPDATE command instead of INSERT INTO.
   
Comments
alfred sanz 4-Feb-13 21:20pm
   
Thanks for this, how about the value I want to insert is in the textbox, how can I do it? I think its with the ")" and "'" code? how's that? thanks :)
alfred sanz 4-Feb-13 22:39pm
   
Hi, can you tell me what will I put in the VALUES (VALUE1, VALUE2)? will I put the VALUES(textBox1.text, textBox2.text) is this it?
Jibesh 5-Feb-13 0:03am
   
values are nothing but the text inputs, replace VALUE1 from textBox1.Text.
Rate this:
Please Sign up or sign in to vote.

Solution 1

There really are no good reasons to use access anymore, as you can download Download SQL Server 2012 Express with SP1[^].

Access was kind of nice about 20 years ago - when you had to part with some serious money to get a serious database server.

And there are many, many articles here on CP, that shows you how to do what you want with SQL server.

Best regards
Espen Harlinn
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100