【问题标题】:Searching a particular letters or words from the string of database从数据库字符串中搜索特定的字母或单词
【发布时间】:2018-01-03 04:13:24
【问题描述】:

我有一个名为 sang.mdf 的数据库,它包含 slno、日期、名称、金额等字段。我想搜索在数据库名称字段中输入的数据。即,如果字段名称包含数据“Abijith”。然后我在我想从数据库中查看“Abijith”的文本框中搜索“Abi”。你能帮帮我吗..

sql connection con = new sqlconnection(@"connection string");
string abi = "select billno, date, name, total from sannew where name = '" + textBox3.Text + "'";
                SqlDataAdapter sdaa = new SqlDataAdapter(abi, con);
                DataSet dss = new DataSet();
                sdaa.Fill(dss);
                dataGridView1.DataSource = dss.Tables[0];

在这种方法中,我想输入全文“Abijith”进行搜索和查看。但是当我搜索“Abi”时,没有找到搜索项

【问题讨论】:

  • 代码在哪里?您在使用代码时遇到什么问题?你知道如何使用 ADO.NET 类从数据库中查询数据吗?
  • 我需要 c# Visual Studio Windows 窗体中的代码
  • sqlconnection con = new sqlconnection(@"connection string"); string abi = "select billno, date, name, total from sannew where name = '" + textBox3.Text + "'"; SqlDataAdapter sdaa = new SqlDataAdapter(abi, con);数据集 dss = new DataSet(); sdaa.填充(dss); dataGridView1.DataSource = dss.Tables[0];
  • 为什么不把这段代码放在你的问题中?

标签: c# sql visual-studio search windows-forms-designer


【解决方案1】:

在查询中使用 LIKE 运算符和 % 通配符。

"select billno, date, name, total from sannew where name LIKE '%" + textBox3.Text + "%'";

注意:这种将用户文本输入中的字符串连接到查询中的方法很糟糕,原因有很多,其中一个原因是您容易受到SQL Injection 的攻击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多