这里采用在visual studio 2010中通过MySql.Data.dll、MySql.Web.dll来连接mysql数据库, 

之后便进行数据的插入和查询。 

Program.cs文件内容如下: 

C#代码  收藏代码
  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.   
  8. namespace DatabaseProgram  
  9. {  
  10.     class Program  
  11.     {  
  12.         static string mysqlConnectionString = "Server=localhost;Database=csharp;Uid=root;Pwd=admin";  
  13.         static void Main(string[] args)  
  14.         {  
  15.             InsertData();  
  16.             SelectData();  
  17.         }  
  18.   
  19.         //向数据库插入数据  
  20.         public static void InsertData()   
  21.         {  
  22.             MySqlConnection conn = new MySqlConnection(mysqlConnectionString);  
  23.             MySqlCommand command;  
  24.             conn.Open();  
  25.             try  
  26.             {  
  27.                 command = conn.CreateCommand();  
  28.                 command.CommandText = "INSERT INTO phoneBook(id,name,mobile) VALUES(@id,@name,@mobile)";  
  29.                 command.Parameters.AddWithValue("@id", 2);  
  30.                 command.Parameters.AddWithValue("@name", "yangjianzhou");  
  31.                 command.Parameters.AddWithValue("@mobile", "1234567890");  
  32.                 command.ExecuteNonQuery();  
  33.             }  
  34.             catch (Exception)  
  35.             {  
  36.             }  
  37.             finally  
  38.             {  
  39.                 if (conn.State == ConnectionState.Open)  
  40.                 {  
  41.                     conn.Close();  
  42.                 }  
  43.             }  
  44.         }  
  45.   
  46.         //从数据库中读取数据  
  47.         public static void SelectData()   
  48.         {  
  49.             MySqlConnection conn = new MySqlConnection(mysqlConnectionString);  
  50.             conn.Open();  
  51.             MySqlDataReader dataReader = null;  
  52.             MySqlCommand command = null;  
  53.             try  
  54.             {  
  55.                 command = conn.CreateCommand();  
  56.                 command.CommandText = "SELECT * FROM phoneBook";  
  57.                 dataReader =command.ExecuteReader();  
  58.                     Console.WriteLine();  
  59.                     while (dataReader.Read())  
  60.                     {  
  61.                         Console.WriteLine("id={0},  name={1},  mobile={2}", dataReader.GetInt16(0), dataReader.GetString(1), dataReader.GetString(2));  
  62.                         Console.WriteLine();  
  63.                     }  
  64.             }  
  65.             catch (Exception)  
  66.             {  
  67.             }  
  68.             finally   
  69.             {  
  70.                 if (!dataReader.IsClosed)  
  71.                 {  
  72.                     dataReader.Close();  
  73.                 }  
  74.                 if (conn.State == ConnectionState.Open)   
  75.                 {  
  76.                     conn.Close();  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81. }  



运行结果如下: 

数据库中内容为: 

 

查询结果为: 

 

posted on 2014-07-18 11:12  GC2013  阅读(6364)  评论(0编辑  收藏  举报