3

I have created an access file with a table named "login". The columns are username and password.

Now i want to add new members to the table with their username and password. The information will be obtained from textbox1 and textbox2.

How can i do this?

By the way, what is the connection string? Is it from the database properties? what i get is "@Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\qianren\Desktop\iplan version\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\log.accdb". Is it correct? Is it the right way to write the connectionstring?

1
  • Are you using the Entity Framework or ADO.NET? Something else? Commented Mar 25, 2012 at 16:46

1 Answer 1

3

First of all you need to add a reference to Microsoft.Office.Interop.Access under the .Net tab in the 'Add Reference' dialog box in Visual Studio.

Then create two variables, one for your textbox1 text, another for your textbox2 text like so:

var foo = textbox1.Text;
var bar = textbox2.Text;

Then add using Microsoft.Office.Interop.Access; to your using statements.

Then you need to make a new instance of the Application class so add the following to your method:

var ap = new Microsoft.Office.Interop.Access.Application();

Then open your database like so:

ap.OpenCurrentDatabase(@"C:\Users\qianren\Desktop\iplan version\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\log.accdb");

Then run an insert query like so:

ap.DoCmd.RunSQL(String.Format("INSERT INTO login ( Username, [Password] ) SELECT "{0}" AS LG, "{1}" AS PW;", foo, bar));

If you are unsure of the syntax, you can design this query using the Microsoft Access query designer, click on the SQL button on the bottom right hand side of the screen and copy the SQL over.

Then close your database like so:

ap.CloseCurrentDatabase();

Finally, clean up your unmanaged object like this:

Marshal.ReleaseComObject(ap); //Sorry this is nearly a year late, just noticed it!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.