C# 遍历 MySQL 表格,统计匹配指定数据的行数
你可以使用 MySQL 的 SELECT COUNT(*) 语句来实现这个功能。以下是一个示例代码:
using System;
using MySql.Data.MySqlClient;
class Program
{
static void Main()
{
string connectionString = 'your_connection_string';
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
string[] givenData = { 'data1', 'data2', 'data3' };
// 构建查询语句
string query = "SELECT COUNT(*) FROM your_table WHERE column_name IN ('" + string.Join("',", givenData) + "')";
MySqlCommand command = new MySqlCommand(query, connection);
int rowCount = Convert.ToInt32(command.ExecuteScalar());
Console.WriteLine("行的个数: " + rowCount);
}
catch (Exception ex)
{
Console.WriteLine("发生错误: " + ex.Message);
}
finally
{
connection.Close();
}
}
}
请注意,你需要将your_connection_string替换为你的MySQL连接字符串,your_table替换为你要查询的表名,column_name替换为你要比较的列名。givenData是一个包含给定数据的字符串数组。
原文地址: https://www.cveoy.top/t/topic/kdo4 著作权归作者所有。请勿转载和采集!