Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!

C#
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 :)

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

C#
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.
 
Share this answer
 
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.
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
 
Share this answer
 

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