C# DbHelperMySQL 详细代码
时间: 2023-09-18 15:10:25 浏览: 50
以下是一个简单的 C# DbHelperMySQL 工具类的代码示例,其中包括了数据库连接、查询、插入、更新和删除等基本操作:
```csharp
using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
public class DbHelperMySQL
{
private static string connectionString = "server=localhost;database=mydatabase;uid=root;pwd=password;";
public static DataTable ExecuteDataTable(string sql, params MySqlParameter[] parameters)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
{
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
}
}
public static int ExecuteNonQuery(string sql, params MySqlParameter[] parameters)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
connection.Open();
return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string sql, params MySqlParameter[] parameters)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
connection.Open();
return command.ExecuteScalar();
}
}
}
}
```
使用时,只需将上述代码放入项目中,并修改 `connectionString` 字符串,即可使用。例如:
```csharp
// 查询数据表
DataTable dataTable = DbHelperMySQL.ExecuteDataTable("SELECT * FROM users");
// 插入数据
MySqlParameter[] parameters = new MySqlParameter[]
{
new MySqlParameter("@name", "John"),
new MySqlParameter("@age", 30)
};
int result = DbHelperMySQL.ExecuteNonQuery("INSERT INTO users(name, age) VALUES(@name, @age)", parameters);
// 更新数据
parameters = new MySqlParameter[]
{
new MySqlParameter("@id", 1),
new MySqlParameter("@name", "Mary"),
new MySqlParameter("@age", 25)
};
result = DbHelperMySQL.ExecuteNonQuery("UPDATE users SET name=@name, age=@age WHERE id=@id", parameters);
// 删除数据
parameters = new MySqlParameter[]
{
new MySqlParameter("@id", 1)
};
result = DbHelperMySQL.ExecuteNonQuery("DELETE FROM users WHERE id=@id", parameters);
```
需要注意的是,上述代码示例中的 `MySqlParameter` 类是用于传递参数的,可以根据实际情况进行使用。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)