C# 操作MySQL实例教程代码,发给喜欢代码的朋友,呵呵。
本文使用MySQLDriverCS操作MYSQL数据库,使用前请先引入MySQLDriverCS.dll文件,支持PHP4.0和PHP5.0
C#代码
- static void Main(string[] args)
- {
- string sqlstr = "select * from manavatar";
- MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString);
- DBConn.Open();
- //MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn);
- MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn);
- MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx();
- MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn);
- DataSet myDataSet = new DataSet();
- DTAdapter.Fill(myDataSet,"manavatar");
- try
- {
- while (DBReader.Read())
- {
- //Console.WriteLine("11");
- Console.WriteLine("DBReader:{0},\t\t\tddddd:{1},\t\t {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));
- }
- Console.WriteLine("0000");
- }
- catch (Exception e)
- {
- Console.WriteLine("读入失败!"+e.ToString());
- }
- finally
- {
- Console.WriteLine("DBReader关闭");
- Console.WriteLine("DBConn关闭");
- DBReader.Close();
- //DBConn.Close();
- }
- for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)
- {
- Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]);
- }
- }
6 条评论
很好用啊,感谢啊
[quote=angus]MySQLDriverCS.dll
这个引入哪里[/quote]直接引入就可以了.
MySQLDriverCS.dll
这个引入哪里
建立连接的速度很慢,这个是为什么,是这种方法的问题,还是我的问题?
兄弟,交换链接,我的站PR=3
http://www.coolblood.cn
已经做好你的链接了;
最近在做毕业时间,呵,用的也是ASP.NET/C#开发;