色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

c#mysql數據增刪改查

錢淋西2年前10瀏覽0評論

在使用C#進行MySQL數據庫的數據增刪改查時,需要使用MySQL官方提供的Connector/NET驅動程序。驅動程序可以通過NuGet包管理器安裝。

創建MySQL連接:

//引入MySQL的命名空間
using MySql.Data.MySqlClient;
//創建連接
string connectionString = "server=127.0.0.1;user id=root;password=123456;database=test";
MySqlConnection conn = new MySqlConnection(connectionString);

數據查詢:

//查詢語句
string sql = "SELECT * FROM user WHERE username = @username";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", "張三");
//執行查詢
MySqlDataReader reader = cmd.ExecuteReader();
//讀取查詢結果
while (reader.Read())
{
Console.WriteLine(reader.GetInt32("id"));
Console.WriteLine(reader.GetString("username"));
Console.WriteLine(reader.GetString("password"));
}
reader.Close();

數據插入:

//插入語句
string sql = "INSERT INTO user(username, password) VALUES(@username, @password)";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", "張三");
cmd.Parameters.AddWithValue("@password", "123456");
//執行插入操作
int result = cmd.ExecuteNonQuery();
Console.WriteLine("插入{0}條數據", result);

數據更新:

//更新語句
string sql = "UPDATE user SET password=@password WHERE username=@username";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", "張三");
cmd.Parameters.AddWithValue("@password", "654321");
//執行更新操作
int result = cmd.ExecuteNonQuery();
Console.WriteLine("更新{0}條數據", result);

數據刪除:

//刪除語句
string sql = "DELETE FROM user WHERE username=@username";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", "張三");
//執行刪除操作
int result = cmd.ExecuteNonQuery();
Console.WriteLine("刪除{0}條數據", result);