C # operate MySQL database ----- HelloWorld,

Source: Internet
Author: User

C # operate MySQL database ----- HelloWorld,

In visual studio 2010, MySql. Data. dll and MySql. Web. dll are used to connect to the mysql database,

Then insert and query the data.

The content of the Program. cs file is as follows:

C # code  
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Linq;
  4. Using System. Text;
  5. Using MySql. Data. MySqlClient;
  6. Using System. Data;
  7. Namespace DatabaseProgram
  8. {
  9. Class Program
  10. {
  11. Static string mysqlConnectionString = "Server = localhost; Database = csharp; Uid = root; Pwd = admin ";
  12. Static void Main (string [] args)
  13. {
  14. InsertData ();
  15. SelectData ();
  16. }
  17. // Insert data into the database
  18. Public static void InsertData ()
  19. {
  20. MySqlConnection conn = new MySqlConnection (mysqlConnectionString );
  21. MySqlCommand command;
  22. Conn. Open ();
  23. Try
  24. {
  25. Command = conn. CreateCommand ();
  26. Command. CommandText = "insert into phoneBook (id, name, mobile) VALUES (@ id, @ name, @ mobile )";
  27. Command. Parameters. AddWithValue ("@ id", 2 );
  28. Command. Parameters. AddWithValue ("@ name", "yangjianzhou ");
  29. Command. Parameters. AddWithValue ("@ mobile", "1234567890 ");
  30. Command. ExecuteNonQuery ();
  31. }
  32. Catch (Exception)
  33. {
  34. }
  35. Finally
  36. {
  37. If (conn. State = ConnectionState. Open)
  38. {
  39. Conn. Close ();
  40. }
  41. }
  42. }
  43. // Read data from the database
  44. Public static void SelectData ()
  45. {
  46. MySqlConnection conn = new MySqlConnection (mysqlConnectionString );
  47. Conn. Open ();
  48. MySqlDataReader dataReader = null;
  49. MySqlCommand command = null;
  50. Try
  51. {
  52. Command = conn. CreateCommand ();
  53. Command. CommandText = "SELECT * FROM phoneBook ";
  54. DataReader = command. ExecuteReader ();
  55. Console. WriteLine ();
  56. While (dataReader. Read ())
  57. {
  58. Console. writeLine ("id = {0}, name = {1}, mobile = {2}", dataReader. getInt16 (0), dataReader. getString (1), dataReader. getString (2 ));
  59. Console. WriteLine ();
  60. }
  61. }
  62. Catch (Exception)
  63. {
  64. }
  65. Finally
  66. {
  67. If (! DataReader. IsClosed)
  68. {
  69. DataReader. Close ();
  70. }
  71. If (conn. State = ConnectionState. Open)
  72. {
  73. Conn. Close ();
  74. }
  75. }
  76. }
  77. }
  78. }



The running result is as follows:

The database content is:

 

The query result is:

 


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Related Article

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.