C # What code is used to connect to the MySQL database?

Source: Internet
Author: User

This article describes the actual application code of C # connecting to the MySQL database. If you are interested in the actual operation steps of C # connecting to the MySQL database, you can get a better understanding of the following articles.

 
 
  1. {
  2. String sqlstr = "select * from manavatar ";
  3. MySQL (the best combination with PHP) Connection DBConn = new MySQL (the best combination with PHP) Connection
    (New MySQL (best combination with PHP) ConnectionString ("192.168.0.13", "flashdata", "root", "root", 3306). AsString );
  4. DBConn. Open ();
  5. // MySQL (the best combination with PHP) DataAdapter myadap = new MySQL (the best combination with PHP) DataAdapter (sqlstr, conn );
  6. MySQL (the best combination with PHP) Command DBComm = new MySQL (the best combination with PHP) Command (sqlstr, DBConn );
  7. MySQL (the best combination with PHP) DataReader DBReader = DBComm. ExecuteReaderEx (); // DBComm. ExecuteReaderEx ();
  8. MySQL (the best combination with PHP) DataAdapter DTAdapter = new MySQL (the best combination with PHP) DataAdapter (sqlstr, DBConn );
  9. DataSet myDataSet = new DataSet ();
  10. DTAdapter. Fill (myDataSet, "manavatar ");
  11. Try
  12. {
  13. While (DBReader. Read ())
  14. {
  15. // Console. WriteLine ("11 ");
  16. Console. WriteLine ("DBReader: {0}, \ t \ tddddd: {1}, \ t {2}", DBReader. GetString (0 ),
    DBReader. GetString (1), DBReader. GetString (3 ));
  17. }
  18. Console. WriteLine ("0000 ");
  19. }
  20. Catch (Exception e)
  21. {
  22. Console. WriteLine ("failed to read! "+ E. ToString ());
  23. }
  24. Finally
  25. {
  26. Console. WriteLine ("DBReader disabled ");
  27. Console. WriteLine ("DBConn disabled ");
  28. DBReader. Close ();
  29. // DBConn. Close ();
  30. }
  31. For (int I = 0; I <myDataSet. Tables ["manavatar"]. Rows. Count; I ++)
  32. {
  33. Console. WriteLine ("{0}", myDataSet. Tables ["manavatar"]. Rows [2] ["user"]);
  34. }
  35. }

This is a simple example.

There is a problem here: if dataset does not set a primary key, it may cause some log library operation problems, such as updata errors.

The above content is an introduction to connecting C # to MySQL. I hope you will get some benefits.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.