How to import a Microsoft Access database into mySQL by using c#

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

How to import a Microsoft Access database into mySQL by using c#

 
0
  #1
32 Days Ago
Hello everybody,

I'm trying to import an access database to mySQL by using c#, I'm using this software: MS Access to MySQL from Bullzip, and it seems to be working ok; by using c#, I can display all data from tables, using this connection:

  1. private void btndisplayBooks_Click(object sender, EventArgs e)
  2. {
  3. String chainCnx = "Server=localhost; Port=3306; Database=library;Uid=root;Pwd=dark;";
  4. MySqlConnection cnx = new MySqlConnection(chainCnx);
  5. MySqlCommand cmd = new MySqlCommand("Select * from books;", cnx);
  6. DataTable dt = new DataTable();
  7. try
  8. {
  9. cnx.Open();
  10. dt.Load(cmd.ExecuteReader());
  11. dgvSearchResult.DataSource = dt; //Gridview
  12. }
  13. catch (Exception)
  14. {
  15. MessageBox.Show("ERROR");
  16. }
  17. finally
  18. {
  19. cnx.Close();
  20. }
  21. }



That seems to be working very well, finally I can display all data from tables, know, my problem is when I try to insert data from this GUI or a stored procedure executing this action, it seems to be not working, by using c# or MySQL Administrator and QueryBrowser, both gave me an error like this: Column count doesn't match value count at row 1


By using c#, I build this connection to insert data:

  1. private void btnInsertarUser_Click(object sender, EventArgs e)
  2. {
  3.  
  4. String chain2 = "Server=localhost;Port=3306;Database=library;Uid=root;Pwd=dark;";
  5. MySqlConnection cnx2 = new MySqlConnection(chain2);
  6.  
  7. String sql = "insert into users(u_UserID, u_Name, u_lastName, u_Address, u_Tel, u_Notes, u_Email) values('"
  8. + txtUserID.Text +
  9. "','"
  10. + txtName.Text +
  11. "','"
  12. + txtLastName.Text +
  13. "','"
  14. + txtTel.Text +
  15. "','"
  16. + txtNotes +
  17. "','"
  18. + txtEmail.Text +
  19. "' );";
  20.  
  21. MySqlCommand cmd = new MySqlCommand(sql, cnx2);
  22. //try
  23. //{
  24. cnx2.Open();
  25. cmd.ExecuteNonQuery();
  26. // }
  27. // catch (Exception)
  28. // {
  29. // MessageBox.Show("ERROR");
  30. // }
  31. // finally
  32. // {
  33. // cnx2.Close();
  34. // }
  35. }



By using a Stored Procedure in QueryBrowser

  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertData`()
  2. BEGIN
  3. insert into users(u_UserID, u_Name, u_lastName, u_Address, u_Tel, u_Notes, u_Email) values('T0120', 'F. John.', 'SMITH', 'SmallVille, '619 570 1053', 'STUDENT', 'f24@gmail.com');
  4. END
  5.  


The error is: Column count doesn't match value count at row 1



Any help would be appreciated
Last edited by NatalyC; 32 Days Ago at 4:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
31 Days Ago
That error explains it. Your column count does not match your value count.

>>insert into users(u_UserID, u_Name, u_lastName, u_Address, u_Tel, u_Notes, u_Email)
1. UserId
2. User Name
3. Last Name
4. Address
5. Telephone
6. Notes
7. Email

>>txtUserID.Text + "','" + txtName.Text + "','" + txtLastName.Text + "','" + txtTel.Text + "','" + txtNotes + "','" + txtEmail.Text + "' );";
1. User Id
2. Name
3. Last Name
4. Telephone
5. Notes
6. Email

As you can see you're specifying data for 7 columns and providing data for 6. You left the address column out of your data.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster
 
0
  #3
31 Days Ago
Thank you very much sknake

I think that was my problem, but I found another solution to insert data into the database, here is my solution:


  1. private void btnInsertBook_Click(object sender, EventArgs e)
  2. {
  3. string myConnectionString2 = "";
  4.  
  5.  
  6. myConnectionString2 = "Server=localhost; Port=3306; Database=library;Uid=root;Pwd=dark;";
  7.  
  8.  
  9. MySqlConnection myConnection2 = new MySqlConnection(myConnectionString2);
  10. string myInsertQuery2 = "INSERT INTO books(l_BookID, l_Title, l_Author, l_Publisher, l_Subject, l_Count) Values(?l_BookID, ?l_Title, ?l_Author, ?l_Publisher, ?l_Subject, ?l_Count)";
  11. MySqlCommand myCommand2 = new MySqlCommand(myInsertQuery2);
  12. myCommand2.Parameters.Add("?l_BookID", MySqlDbType.VarChar, 255).Value = txtISBNBooks.Text;
  13. myCommand2.Parameters.Add("?l_Title", MySqlDbType.VarChar, 255).Value = txtTitle.Text;
  14. myCommand2.Parameters.Add("?l_Author", MySqlDbType.VarChar, 255).Value = txtAuthor.Text;
  15. myCommand2.Parameters.Add("?l_Publisher", MySqlDbType.VarChar, 255).Value = txtPublisher.Text;
  16. myCommand2.Parameters.Add("?l_Subject", MySqlDbType.VarChar, 255).Value = txtSubject.Text;
  17. myCommand2.Parameters.Add("?l_Count", MySqlDbType.Int16,11).Value = txtCountBooks.Text;
  18.  
  19.  
  20.  
  21. myCommand2.Connection = myConnection2;
  22. myConnection2.Open();
  23. myCommand2.ExecuteNonQuery();
  24. myCommand2.Connection.Close();
  25. }




But, know . . . my problem is when I try to delete some data
Here is my code about deleting data from a database:


  1. private void btnRemoveBook_Click(object sender, EventArgs e)
  2. {
  3.  
  4. string myConnectionString="";
  5.  
  6. myConnectionString = "Server=localhost; Port=3306; Database=library;Uid=root;Pwd=dark;";
  7.  
  8. MySqlConnection myConnection = new MySqlConnection(myConnectionString);
  9. string myInsertQuery = "DELETE FROM books WHERE l_BookID=" + txtISBNDeleteBook.Text + "";
  10. MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
  11.  
  12.  
  13. myCommand.Connection = myConnection;
  14. myConnection.Open();
  15. myCommand.ExecuteNonQuery();
  16. myCommand.Connection.Close();
  17.  
  18. }


I don´t know why is not working, no sintaxis error and nothing else, just that is not working


Could anybody help me ?


Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
31 Days Ago
You do realize you just gave out your root password, right?

You should change your delete query to use parameterized SQL like your modified insert query and try that.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster
 
0
  #5
31 Days Ago
Could you please give me a hand? I don´t know how to do that in case of deleting data.


I think I can use this sentence:
  1. string myInsertQuery = "DELETE FROM books WHERE l_BookID=" + txtISBNDeleteBook.Text + "";

but that seems to be the problem, I don't know how to use parameters in this case.


Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 886
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 140
DdoubleD DdoubleD is offline Offline
Practically a Posting Shark
 
0
  #6
31 Days Ago
Try this:

  1. public static int DeleteRecord(string bookID)
  2. {
  3. const string query = "DELETE FROM Books WHERE l_bookID = @l_bookID";
  4. using (SqlConnection conn = new SqlConnection(connStr))
  5. {
  6. conn.Open();
  7. using (SqlCommand cmd = new SqlCommand(query, conn))
  8. {
  9. cmd.Parameters.Add(new SqlParameter("@l_bookID", SqlDbType.VarChar)).Value = bookID;
  10. return cmd.ExecuteNonQuery();
  11. }
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster
 
0
  #7
31 Days Ago
It works !!!!

Thank you very much

I've uploaded my project in a pdf file for those who want to know something about MySQL, the only thing is that is in Spanish, but if you have any doubt, you can ask me.

Thanks again !!!!
Attached Files
File Type: pdf MySQLBiblioteca1.pdf (37.1 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 886
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 140
DdoubleD DdoubleD is offline Offline
Practically a Posting Shark
 
0
  #8
31 Days Ago
That was very nice that you posted your program/solution so everyone could see it. Couple of suggestions:
1) you define your connection string in multiple places... consider just creating a single "const string" definition for the whole class
2) be careful about posting details of your connection string, especially for "root" uid and password; it's better you don't share this information with the world.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster
 
0
  #9
31 Days Ago
Ok I've changed my root and pass, so thanks for the advice, I'll consider that for next time


I hope to help somebody else, so thanks again.

Reply With Quote Quick reply to this message  
Reply

Tags
access, c#, database, mysql

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC