欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

c#mysql數(shù)據(jù)增刪改查

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

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

創(chuàng)建MySQL連接:

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

數(shù)據(jù)查詢:

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

數(shù)據(jù)插入:

//插入語句
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");
//執(zhí)行插入操作
int result = cmd.ExecuteNonQuery();
Console.WriteLine("插入{0}條數(shù)據(jù)", result);

數(shù)據(jù)更新:

//更新語句
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");
//執(zhí)行更新操作
int result = cmd.ExecuteNonQuery();
Console.WriteLine("更新{0}條數(shù)據(jù)", result);

數(shù)據(jù)刪除:

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