C#中连接MySQL数据
2013-09-28 16:22
281 查看
小结一下MySQL在C#中是如何连接的,并做一些简单的选择(SELECT)、插入( INSERT)、更新( UPDATE)、删除(DELETE )
(一)连接
a) Firstly, you should install MySQL. To use the methods in the MySQL Connector/NET you should add a reference to it. Right click your project in the Solution Explorer and click Add Reference… In the .NET tab, chose MySql.Data and click ok.
b) In your code, you should add the line using MySql.Data.MySqlClient; in order to be able to use methods for accessing MySql.
c) To Connect to a MySql Database:
localhost是服务器地址。
(二)其他操作
a) 执行已准备的一些MySQL指令To code some MySQL commands using Prepared Statements:
*Note: You can also try this kind of cmdText, “INSERT INTO myTable VALUES(@name,@age,@contact)”;
Then add the cmd.Parameters.AddWithValue for each placeholder @name,@age,@contact.
If you don't want a column in your table to be bypassed you may use NULL. e.g. “INSERT INTO myTable VALUES(NULL,@name,@age,@contact)”;
b) 使用MySqlDataReader检索数据To Retrieve Data using MySqlDataReader:
c) 执行某一些SQL语句To Execute Some MySql Statements:
SELECT:
UPDATE:
DELETE:
注:在使用数据库前,必须要确保MySQL数据库服务已经打开(通过con.Open()),并一定要安全地关闭(con.Close())。
Adjusted from : http://forum.codecall.net/topic/71422-connecting-to-a-mysql-database-in-c/
(一)连接
a) Firstly, you should install MySQL. To use the methods in the MySQL Connector/NET you should add a reference to it. Right click your project in the Solution Explorer and click Add Reference… In the .NET tab, chose MySql.Data and click ok.
b) In your code, you should add the line using MySql.Data.MySqlClient; in order to be able to use methods for accessing MySql.
using MySql.Data.MySqlClient;
c) To Connect to a MySql Database:
String str = @"server=localhost;database=yourDBname;userid=root;password=yourDBpassword;"; MySqlConnection con = null; try { con = new MySqlConnection(str); con.Open(); //open the connection } catch (MySqlException err) //We will capture and display any MySql errors that will occur { Console.WriteLine("Error: " + err.ToString()); } finally { if (con != null) { con.Close(); //safely close the connection } } //remember to safely close the connection after accessing the database
localhost是服务器地址。
(二)其他操作
a) 执行已准备的一些MySQL指令To code some MySQL commands using Prepared Statements:
String str = @"server=localhost;database=yourDBname;userid=root;password=yourDBpassword;"; MySqlConnection con = null; try { con = new MySqlConnection(str); con.Open(); //open the connection //This is the mysql command that we will query into the db. //It uses Prepared statements and the Placeholder is @name. //Using prepared statements is faster and secure. String cmdText = "INSERT INTO myTable(name) VALUES(@name)"; MySqlCommand cmd = new MySqlCommand(cmdText,con); cmd.Prepare(); //we will bound a value to the placeholder cmd.Parameters.AddWithValue("@name", "your value here"); cmd.ExecuteNonQuery(); //execute the mysql command } catch (MySqlException err) { Console.WriteLine("Error: " + err.ToString()); } finally { if (con != null) { con.Close(); //close the connection } } //remember to close the connection after accessing the database
*Note: You can also try this kind of cmdText, “INSERT INTO myTable VALUES(@name,@age,@contact)”;
Then add the cmd.Parameters.AddWithValue for each placeholder @name,@age,@contact.
If you don't want a column in your table to be bypassed you may use NULL. e.g. “INSERT INTO myTable VALUES(NULL,@name,@age,@contact)”;
b) 使用MySqlDataReader检索数据To Retrieve Data using MySqlDataReader:
String str = @"server=localhost;database=yourDBname;userid=root;password=yourDBpassword;"; MySqlConnection con = null; //MySqlDataReader Object MySqlDataReader reader = null; try { con = new MySqlConnection(str); con.Open(); //open the connection //We will need to SELECT all or some columns in the table //via this command String cmdText = "SELECT * FROM myTable"; MySqlCommand cmd = new MySqlCommand(cmdText,con); reader = cmd.ExecuteReader(); //execure the reader /*The Read() method points to the next record It return false if there are no more records else returns true.*/ while (reader.Read()) { /*reader.GetString(0) will get the value of the first column of the table myTable because we selected all columns using SELECT * (all); the first loop of the while loop is the first row; the next loop will be the second row and so on...*/ Console.WriteLine(reader.GetString(0)); } } catch (MySqlException err) { Console.WriteLine("Error: " + err.ToString()); } finally { if (reader != null) { reader.Close(); } if (con != null) { con.Close(); //close the connection } } //remember to close the connection after accessing the database
c) 执行某一些SQL语句To Execute Some MySql Statements:
SELECT:
//This is the simple code of executing MySql Commands in C# String cmdText = "SELECT id,name,contact FROM myTable"; //This line is the MySql Command MySqlCommand cmd = new MySqlCommand(cmdText, con); cmd.ExecuteNonQuery(); //Execute the command
UPDATE:
//example on how to use UPDATE cmd = new MySqlCommand("UPDATE ab_data SET banned_from='" + from + "' , banned_until='" + until + "' WHERE name='" + name + "'", con); cmd.ExecuteNonQuery();
DELETE:
//example on how to use DELETE cmd = new MySqlCommand("DELETE FROM tbName WHERE colName = someValue",con); cmd.ExecuteNonQuery();
注:在使用数据库前,必须要确保MySQL数据库服务已经打开(通过con.Open()),并一定要安全地关闭(con.Close())。
Adjusted from : http://forum.codecall.net/topic/71422-connecting-to-a-mysql-database-in-c/
相关文章推荐
- c#连接MYSQL数据
- C#连接MySQL读取数据乱码
- C# MySQL连接及数据写入和检索
- C#连接mysql数据及简单操作
- c# 连接mysql并webservice数据
- mysql字符串连接和批量插入数据
- 【转】matlab 连接 mysql 数据库
- C#连接mysql 数据库
- java 单例模式(singleton)连接 mysql 数据库
- php通过Mysqli和PDO连接mysql数据详解
- C#(.NET)数据访问连接、查询、插入等操作的封装类
- C#如何连接ACCESS数据库,并在里面去数据!!
- c# 读取文本中的一行用逗号连接数据,并对其进行排序
- c#连接mysql实例
- C#连接Access数据库,Update更新数据时出现“不能打开数据库,应用程序可能无法识别该数据库。。。”错误
- C#连接Mysql之MySQLDriverCS
- Java连接MySQL数据
- [转]C#连接操作mysql实例
- Python爬虫数据存储MySQL【1】连接方式
- 4、MySQL 8.0参考手册 连接到服务器并断开连接3.3.3将数据加载到表中