zoukankan      html  css  js  c++  java
  • C#访问MySQL数据库的方法

    C#访问MySQL数据库的方法

    (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序

    下载地址为:

    http://dev.mysql.com/downloads/connector/net/6.0.html

    我下载的版本为: mysql-connector-net-6.3.8.msi

    下载地址如下url:

    http://dev.mysql.com/downloads/mirror.php?id=405442


    (2)安装mysql-connector-net

    然后直接在Windows操作系统安装 mysql-connector-net-6.3.8.msi

    默认是安装在C盘:

    C:Program FilesMySQLMySQL Connector Net 6.3.8Assemblies

    v2.0

    v4.0

    安装完后我选择的是v2.0版本的

    然后在应用工程中引用组件MySQL.Data.dll


    (3)封装数据库访问组件DbConnectionMySQL

    [csharp] view plaincopy
    1. /// <summary>  
    2.     /// MySQL数据库   
    3.     /// 版本 mysql-connector-net-6.3.8.msi  
    4.     /// vp:hsg  
    5.     /// create date:2012-02-28  
    6.     /// </summary>  
    7.     [Serializable]  
    8.     public class DbConnectionMySQL : DbConnectionWrapper  
    9.     {  
    10.         public DbConnectionMySQL(string pConnectionString)  
    11.             : base(pConnectionString)  
    12.         {  
    13.               
    14.             this.m_dbconn = new MySqlConnection(pConnectionString);  
    15.             this.m_DbConnState = DbConnState.Free;  
    16.         }  
    17.   
    18.         //--  
    19.         public override DbDataAdapter GetDbDataAdapter()  
    20.         {  
    21.             return new MySqlDataAdapter();  
    22.         }  
    23.         public override DbDataAdapter GetDbDataAdapter(DbCommand dbCommand)  
    24.         {  
    25.             return new MySqlDataAdapter(dbCommand as MySqlCommand);  
    26.         }  
    27.         public override DbCommand GetDbCommand()  
    28.         {  
    29.             return new MySqlCommand();  
    30.         }  
    31.         public override DbConnection GetDbConnection()  
    32.         {  
    33.             return new MySqlConnection();  
    34.         }  
    35.         public override DbCommandBuilder GetDbCommandBuilder()  
    36.         {  
    37.             return new MySqlCommandBuilder();  
    38.         }  
    39.   
    40.         public override DataProviderType GetCurrentDataProviderType()  
    41.         {  
    42.             return DataProviderType.Sql;  
    43.         }  
    44.   
    45.         public override bool IsExistsTable(string TableName, string UserName)  
    46.         {  
    47.             #region information  
    48.             bool rbc = false;    //TABLES表中去查询 table_name  
    49.             string dSql = "select * from TABLES where table_name='" + TableName + "'";  
    50.             DataSet ds = this.ExecuteDataSet(dSql);  
    51.             if (ds != null)  
    52.             {  
    53.                 if (ds.Tables[0].Rows.Count > 0)  
    54.                 {  
    55.                     rbc = true;  
    56.                 }  
    57.                 else  
    58.                 {  
    59.                     rbc = false;  
    60.                 }  
    61.             }  
    62.             else  
    63.             {  
    64.                 rbc = false;  
    65.             }  
    66.             return rbc;  
    67.             #endregion  
    68.         }  
    69.         public override bool IsExistsField(string FieldName, string TableName)  
    70.         {  
    71.             #region information  
    72.             bool rbc = false;  
    73.             string dSql = "";  
    74.             dSql = "select * from " + TableName + " where 1<>1";  
    75.             DataSet ds = this.ExecuteDataSet(dSql);  
    76.             if (ds != null)  
    77.             {  
    78.                 DataTable dt = ds.Tables[0];  
    79.                 for (int j = 0; j < dt.Columns.Count; j++)  
    80.                 {  
    81.                     if (dt.Columns[j].ColumnName.ToString().ToUpper() == FieldName.ToString().ToUpper())  
    82.                     {  
    83.                         rbc = true;  
    84.                         goto Return_End;  
    85.                     }  
    86.                 }  
    87.                 dt.Dispose();  
    88.                 dt = null;  
    89.             }  
    90.             ds.Dispose();  
    91.             ds = null;  
    92.   
    93.         Return_End:  
    94.   
    95.             return rbc;  
    96.             #endregion  
    97.         }  
    98.   
    99.         public override char ParameterChar  
    100.         {  
    101.             get  
    102.             {  
    103.                 return ':';   //SQLite的参数符号为:  
    104.             }  
    105.         }  
    106.   
    107.         public override DbParameter CreateParameter(string name, object value)  
    108.         {  
    109.             return new MySqlParameter(name, value);  
    110.         }  
    111.   
    112.         public override DbParameter CreateParameter(string name)  
    113.         {  
    114.             DbParameter dbp = new MySqlParameter();  
    115.             dbp.ParameterName = name;  
    116.             return dbp;  
    117.         }  
    118.         public override DbParameter CreateParameter(string name, DbType dbtype, object value)  
    119.         {  
    120.             DbParameter dbp = new MySqlParameter();  
    121.             dbp.ParameterName = name;  
    122.             dbp.Value = value;  
    123.             dbp.DbType = dbtype;  
    124.             return dbp;  
    125.         }  
    126.         public override DbParameter CreateParameter(string name, DbType dbtype, int size, object value)  
    127.         {  
    128.             DbParameter dbp = new MySqlParameter();  
    129.             dbp.ParameterName = name;  
    130.             dbp.Value = value;  
    131.             dbp.DbType = dbtype;  
    132.             dbp.Size = size;  
    133.             return dbp;  
    134.         }  
    135.     }  

    (4)客户端访问测试开发实例

    [csharp] view plaincopy
    1. public void TestCShape_MySQL()  
    2.         {  
    3.             string constr = "server=localhost;User Id=root;password=root;Database=xp_users";  
    4.             DbConnectionWrapper dbw = new DbConnectionMySQL(constr);  
    5.             bool rbc=dbw.TestConnection();  
    6.             this.Context.Response.Write(rbc);  
    7.                          
    8.   
    9.             string x = "";  
    10.             //删除语句  
    11.             x = "delete from xp_users";  
    12.             if (dbw.ExecuteQuery(x) > 0)  
    13.             {  
    14.                 this.Context.Response.Write("删除语句成功!下面是SQL语句<br>" + x);  
    15.             }  
    16.             //插入语句  
    17.             x = "insert into xp_users(gid,uid,uname,sex,email,pwd) values('";  
    18.             x += "1','hsg77','何XXX',1,'hsg77@163.com','1')";  
    19.             if (dbw.ExecuteQuery(x) > 0)  
    20.             {  
    21.                 this.Context.Response.Write("插入语句成功!下面是SQL语句<br>"+x);  
    22.             }  
    23.             //查询语句  
    24.             DataTable dt = dbw.ExecuteDataTable("select * from xp_users");  
    25.             if (dt != null && dt.Rows.Count > 0)  
    26.             {  
    27.                 this.Context.Response.Write("<br>用户数:"+dt.Rows.Count);  
    28.             }  
    29.             if (dt != null)  
    30.             {  
    31.                 dt.Dispose();  
    32.                 dt = null;  
    33.             }  
    34.             dbw.Dispose();  
    35.             dbw = null;  
    36.         }  

    ----the---end---

    create date:2012-02-28

  • 相关阅读:
    poj 3321 Apple Tree
    hdu 1520 Anniversary party
    Light OJ 1089 Points in Segments (II)
    Timus 1018 Binary Apple Tree
    zoj 3299 Fall the Brick
    HFUT 1287 法默尔的农场
    Codeforces 159C String Manipulation 1.0
    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
    使用 TypeScript & mocha & chai 写测试代码实战(17 个视频)
    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
  • 原文地址:https://www.cnblogs.com/kevinGao/p/4188731.html
Copyright © 2011-2022 走看看