国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#操作MySQL數據庫-----HelloWorld

2019-11-14 16:33:45
字體:
來源:轉載
供稿:網友

這里采用在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. }  



運行結果如下: 

數據庫中內容為: 

 

查詢結果為: 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河北区| 麻江县| 新营市| 嵩明县| 彰化县| 寻乌县| 昌乐县| 勃利县| 都安| 重庆市| 眉山市| 湘西| 梁平县| 合肥市| 佛山市| 双桥区| 惠东县| 广昌县| 邳州市| 大石桥市| 邛崃市| 读书| 蓬溪县| 神农架林区| 金堂县| 永济市| 积石山| 宣威市| 和硕县| 增城市| 灵武市| 芜湖县| 塔河县| 肇东市| 句容市| 安宁市| 中阳县| 潼南县| 涿鹿县| 九龙坡区| 育儿|