c# mysql 查询数据库的数据
时间: 2023-08-11 12:21:24 浏览: 57
你可以使用 MySQL Connector/NET 连接MySQL数据库,然后使用 C# 代码来查询数据。下面是一个示例代码:
```csharp
using MySql.Data.MySqlClient;
// 创建数据库连接
string connectionString = "server=localhost;user=root;database=mydatabase;port=3306;password=mypassword;";
MySqlConnection connection = new MySqlConnection(connectionString);
// 打开数据库连接
connection.Open();
// 查询数据
string query = "SELECT * FROM mytable";
MySqlCommand command = new MySqlCommand(query, connection);
MySqlDataReader reader = command.ExecuteReader();
// 读取并输出查询结果
while (reader.Read())
{
string column1 = reader.GetString(0);
int column2 = reader.GetInt32(1);
Console.WriteLine("Column 1: {0}, Column 2: {1}", column1, column2);
}
// 关闭数据库连接
reader.Close();
connection.Close();
```
其中,`connectionString` 是连接MySQL数据库的字符串,`query` 是查询语句,`MySqlCommand` 和 `MySqlDataReader` 分别用于执行和读取查询结果。你需要修改 `connectionString` 以及查询语句,来适配你自己的数据库和表格。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)