C#连接mysql数据库的完整方法

来源:互联网 发布:java json解析string 编辑:程序博客网 时间:2024/05/01 05:18

具体步骤(已经最简便了):

1.  下载 libmysql.dll 和 MySQLDriverCS.DLL

    将 libmysql.dll 拷贝进  C:\Windows\System32 下面

2.  在工程添加(选中工程右键,下面添加): MySQLDriverCS.DLL

3.  在工程上面引用: Using  usingMySQLDriverCS;

--------------------------------------------------------

//增,删,改
           MySQLConnection DBConn;
           DBConn = new MySQLConnection(new MySQLConnectionString

("localhost", "super", "root", "123", 3306).AsString);

           DBConn.Open();
           // 执行查询语句 
           MySQLCommand DBComm;
           DBComm = new MySQLCommand("insert into employee values

(null,'"+jobId+"','"+employeeName+"','"+sex+"','"+phone+"','"+salary+"','"

+pwd+"','"+date+"');", DBConn);
           int a = DBComm.ExecuteNonQuery();
           // 显示数据 
           try
           {
               if (a > 0)
               {
                   MessageBox.Show("执行成功!");
               }
               else
               {
                   MessageBox.Show("书写错误,或系统繁忙!");
               }
           }
           finally
                        
               DBConn.Close();
           }
           //关闭数据库连接 
           DBConn.Close();
-------------------------------------------------------------
注释:super:你的数据库的名称
    root:你的数据库账户
    123:你数据库的密码

--------------------------------------------------------------
 //查询

           MySQLConnection DBConn;
           DBConn = new MySQLConnection(new MySQLConnectionString

("localhost", "super", "root", "123", 3306).AsString);

           DBConn.Open();
           // 执行查询语句 
           MySQLCommand DBComm;
           DBComm = new MySQLCommand("select sum(bills_money) from bills

where operation_date='"+date+"';", DBConn);
           //MySQLDataReader
           var DBReader = DBComm.ExecuteReaderEx();
           // 显示数据 
           try
           {
               while (DBReader.Read())
               {
                   this.MoneytextBox.Text = DBReader.GetString(0);
                   if(this.MoneytextBox.Text==""){
                       MessageBox.Show("无盈利!");
                   }

               }

           }
           finally
           {
               DBReader.Close();
               DBConn.Close();
       }
           //关闭数据库连接 
           DBConn.Close();

0 0
原创粉丝点击