C#连接与使用MySql数据库

先下载 MySql.Data.dll,把它引用到项目中

下载地址: http://good.gd/1938514.htm  或

http://www.dllzj.com/Down_MySql.Data.dll.html

 

增加一个方法,测试连接是否成功:

 

[csharp]  view plain copy
  1. public static bool TestConn()  
  2. {   
  3.     MySqlConnection myConn = null;  
  4.     bool bResult = false;  
  5.     try  
  6.     {  
  7.         myConn = new MySqlConnection(_ConnString);  
  8.         myConn.Open();  
  9.     }  
  10.     catch (Exception ex)  
  11.     {  
  12.     }  
  13.     finally   
  14.     {  
  15.         if (myConn != null && myConn.State.ToString() == "Open")  
  16.             bResult = true;  
  17.     }  
  18.   
  19.     myConn.Close();  
  20.   
  21.     return bResult;  
  22. }  


 

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using System.Data;  
  7. using MySql.Data.MySqlClient;  
  8. using System.Configuration;  
  9.   
  10. namespace JonseTest  
  11. {  
  12.    public class MySqlHelper  
  13.     {  
  14.   
  15.        public static string ConnString = "";  
  16.   
  17.        public static string Conn_Config_Str_Name = string.Empty;  
  18.   
  19.        public static string Conn_Server = string.Empty;  
  20.        public static string Conn_DBName = string.Empty;  
  21.        public static string Conn_Uid = string.Empty;  
  22.        public static string Conn_Pwd = string.Empty;  
  23.   
  24.        private static string _ConnString  
  25.        {  
  26.            get  
  27.            {  
  28.                if (!string.IsNullOrEmpty(ConnString))  
  29.                    return ConnString;  
  30.   
  31.                object oConn = ConfigurationManager.ConnectionStrings[Conn_Config_Str_Name];  
  32.                if (oConn != null && oConn.ToString() != "")  
  33.                    return oConn.ToString();  
  34.   
  35.                return string.Format(@"server={0};database={1};userid={2};password={3}", Conn_Server, Conn_DBName, Conn_Uid, Conn_Pwd);  
  36.            }  
  37.        }  
  38.   
  39.        // 读取数据 datatable  
  40.        public static DataTable GetDataTable(out string sError,string sSQL)  
  41.        {  
  42.            DataTable dt = null;  
  43.            sError = string.Empty;  
  44.    
  45.            MySqlConnection myConn = null;  
  46.            try  
  47.            {  
  48.                myConn = new MySqlConnection(_ConnString);  
  49.                MySqlCommand myCommand = new MySqlCommand(sSQL, myConn);  
  50.                myConn.Open();  
  51.                MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);  
  52.                dt = new DataTable();  
  53.                adapter.Fill(dt);  
  54.                myConn.Close();  
  55.            }  
  56.            catch (Exception ex)  
  57.            {  
  58.                sError = ex.Message;  
  59.            }  
  60.            return dt;  
  61.        }  
  62.   
  63.        // 读取数据 dataset  
  64.        public static DataSet GetDataSet(out string sError, string sSQL)  
  65.        {  
  66.            DataSet ds = null;  
  67.            sError = string.Empty;  
  68.   
  69.            MySqlConnection myConn = null;  
  70.            try  
  71.            {  
  72.                myConn = new MySqlConnection(_ConnString);  
  73.                MySqlCommand myCmd = new MySqlCommand(sSQL, myConn);  
  74.                myConn.Open();  
  75.                MySqlDataAdapter adapter = new MySqlDataAdapter(myCmd);  
  76.                ds = new DataSet();  
  77.                adapter.Fill(ds);  
  78.                myConn.Close();  
  79.            }  
  80.            catch (Exception ex)  
  81.            {  
  82.                sError = ex.Message;  
  83.            }  
  84.            return ds;  
  85.        }  
  86.   
  87.        // 取最大的ID  
  88.        public static Int32 GetMaxID(out string sError, string sKeyField, string sTableName)  
  89.        {  
  90.            DataTable dt = GetDataTable(out sError, "select IFNULL(max(" + sKeyField + "),0) as MaxID from " + sTableName );  
  91.            if (dt != null && dt.Rows.Count > 0)  
  92.            {  
  93.                return Convert.ToInt32(dt.Rows[0][0].ToString());  
  94.            }  
  95.   
  96.            return 0;  
  97.        }  
  98.   
  99.        // 插入,修改,删除,是否使用事务  
  100.        public static bool UpdateData(out string sError, string sSQL,bool bUseTransaction=false)  
  101.        {  
  102.            int iResult=0;  
  103.            sError=string.Empty;  
  104.   
  105.            MySqlConnection myConn = null;  
  106.   
  107.            if (!bUseTransaction)  
  108.            {  
  109.                try  
  110.                {  
  111.                    myConn = new MySqlConnection(_ConnString);  
  112.                    MySqlCommand myCmd = new MySqlCommand(sSQL, myConn);  
  113.                    myConn.Open();  
  114.                    iResult = myCmd.ExecuteNonQuery();  
  115.                    myConn.Close();  
  116.                }  
  117.                catch (Exception ex)  
  118.                {  
  119.                    sError = ex.Message;  
  120.                    iResult = -1;  
  121.                }  
  122.            }  
  123.            else // 使用事务  
  124.            {  
  125.                MySqlTransaction myTrans = null;  
  126.                try  
  127.                {  
  128.                    myConn = new MySqlConnection(_ConnString);  
  129.                    myConn.Open();  
  130.                    myTrans = myConn.BeginTransaction();  
  131.                    MySqlCommand myCmd = new MySqlCommand(sSQL, myConn);  
  132.                    myCmd.Transaction = myTrans;  
  133.                    iResult = myCmd.ExecuteNonQuery();  
  134.                    myTrans.Commit();  
  135.                    myConn.Close();  
  136.                }  
  137.                catch (Exception ex)  
  138.                {  
  139.                    sError = ex.Message;  
  140.                    iResult = -1;  
  141.                    myTrans.Rollback();  
  142.                }  
  143.            }  
  144.   
  145.            return iResult>0;  
  146.        }  
  147.   
  148.   
  149.     }  
  150. }  


MySqlHelper类的调用方法与SqlServerHelper类的调用非常相似,请参考:

用于SqlServer数据库的SqlServerHelper.cs类,及其调用例子  

网址: http://blog.csdn.net/keenweiwei/article/details/6845709

 

注意 App.Config中,connectionString 中的是 userid ,不是uid

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConnString" connectionString="server=电脑名 或 电脑IP;database=数据库名;userid=数据库登录名;password=数据库登录密码" />
  </connectionStrings>
</configuration>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值