下面是一个使用 C# 进行模糊查询的示例代码:

using System;
using MySql.Data.MySqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "server=localhost;database=your_database;uid=your_username;password=your_password;";
        string query = "SELECT * FROM your_table WHERE column1 LIKE @searchTerm OR column2 LIKE @searchTerm OR column3 LIKE @searchTerm OR column4 LIKE @searchTerm";

        Console.WriteLine("Enter search term:");
        string searchTerm = Console.ReadLine();

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            connection.Open();

            using (MySqlCommand command = new MySqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@searchTerm", "%" + searchTerm + "%");

                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("Column1: " + reader["column1"]);
                        Console.WriteLine("Column2: " + reader["column2"]);
                        Console.WriteLine("Column3: " + reader["column3"]);
                        Console.WriteLine("Column4: " + reader["column4"]);
                        Console.WriteLine();
                    }
                }
            }
        }

        Console.ReadLine();
    }
}

请注意替换 your_databaseyour_usernameyour_passwordyour_table 为实际的数据库名称、用户名、密码和表名。此代码将在控制台上提示用户输入要搜索的术语,并在匹配的行中打印出四列的值。使用 LIKE 运算符和 % 通配符来进行模糊搜索。

编写代码:c# 实现:MySQL表中含有四列模糊查询表中的值

原文地址: http://www.cveoy.top/t/topic/i9C2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录