cshap 连接sql2008做模糊查询
时间: 2023-05-30 15:08:02 浏览: 139
下面是一个使用C#连接SQL Server 2008进行模糊查询的示例代码:
```
using System;
using System.Data.SqlClient;
namespace FuzzySearchDemo
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True";
string keyword = "search keyword";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT * FROM YourTableName WHERE YourColumnName LIKE @keyword";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@keyword", "%" + keyword + "%");
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["YourColumnName"]);
}
reader.Close();
}
Console.ReadKey();
}
}
}
```
在上面的示例代码中,我们首先定义了SQL Server 2008的连接字符串和要搜索的关键字。然后,我们使用SqlConnection类创建一个连接对象,并使用SqlCommand类创建一个查询对象。我们使用参数化查询来防止SQL注入攻击,并将关键字作为参数传递给查询。接下来,我们打开连接并执行查询,然后使用SqlDataReader类读取查询结果并输出到控制台。最后,我们关闭连接和读取器。
请注意,您需要将示例代码中的“YourServerName”、“YourDatabaseName”、“YourTableName”和“YourColumnName”替换为您的实际值。此外,您还需要确保您的应用程序具有连接到SQL Server 2008数据库的适当权限。
阅读全文
相关推荐















