C#连接MySQL数据库的方法

ershen290

贡献于2013-11-22

字数:0 关键词: MySQL 数据库服务器 C# SQL

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; namespace ss.BaseClass { class BaseClass { /// /// 建立数据库联接 /// /// public MySqlConnection getconn() { string M_Str_sqlconn = "Server=127.0.0.1;Database=testing;Uid=root;Pwd="; MySqlConnection myconn = new MySqlConnection(M_Str_sqlconn); return myconn; } /// /// 执行 SqlCommand /// /// public void getcom(string M_Str_sqlstr) { MySqlConnection myconn = this.getconn(); myconn.Open(); MySqlCommand sqlcom = new MySqlCommand(M_Str_sqlstr,myconn); sqlcom.ExecuteNonQuery(); sqlcom.Dispose(); myconn.Close(); myconn.Dispose(); } /// /// 创建一个 DataSet 对象 /// /// /// /// public DataSet getds(string M_str_sqlstr,string M_str_table) { MySqlConnection myconn = getconn(); myconn.Open(); MySqlDataAdapter sqlda = new MySqlDataAdapter(M_str_sqlstr,myconn); DataSet myds = new DataSet(); sqlda.Fill(myds,M_str_table); return myds; } /// /// 创建一个 SqlDataReader 对象 /// /// /// public MySqlDataReader getread(string M_str_sqlstr) { MySqlConnection myconn = this.getconn(); MySqlCommand mycmd = new MySqlCommand(M_str_sqlstr,myconn); myconn.Open(); MySqlDataReader mysqlread = mycmd.ExecuteReader(CommandBehavior.CloseConnection); return mysqlread; } } }

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档

相关文档