在 C# 窗体的新窗口中查看搜索引擎结果

本文关键字:结果 搜索引擎 窗口 窗体 新窗口 | 更新日期: 2023-09-27 18:35:25

我正在使用一个包含名称公司和国家的组合框,因此用户可以从中进行选择以指定他要在文本框中搜索的形式,我需要以不同的形式查看我的搜索结果(结果.cs)并且我的搜索引擎打开(主要.cs) 我该怎么做?

private void button1_Click(object sender, EventArgs e)
{
  this.Hide();
  if (comboBox1.Text == "Name")
  {
      String var;
      SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Seif-'Documents'Visual Studio 2013'Projects'BusinessCard'BusinessCard'BusinessCards.mdf;Integrated Security=True");
      SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Name LIKE '" + textBox1.Text + "'", conn);
      SqlDataAdapter sda = new SqlDataAdapter(sc);
      DataTable dt = new DataTable();
      sda.Fill(dt);
      var = (string)sc.ExecuteScalar();
      Search f2 = new Search();
      f2.Show();
  }
  else if (comboBox1.Text == "Company")
  {
    String var;
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Seif-'Documents'Visual Studio 2013'Projects'BusinessCard'BusinessCard'BusinessCards.mdf;Integrated Security=True");
    SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Company LIKE '" + textBox1.Text + "'", conn);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();
  }
  else if (comboBox1.Text == "Country")
  {
    String var;
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Seif-'Documents'Visual Studio 2013'Projects'BusinessCard'BusinessCard'BusinessCards.mdf;Integrated Security=True");
    SqlCommand sc = new SqlCommand("SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where Country LIKE '" + textBox1.Text + "'", conn);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();
  }
}

在 C# 窗体的新窗口中查看搜索引擎结果

执行此操作的最常见方法是在 Search 构造函数中传递DataTable

Search f2 = new Search(dt);

Search形式中,您将有一个私有成员来保存该值。

private DataTable _results;
public Search(DataTable table)
{
   _results = table;
}

这样,您就可以在Search的任何地方使用_results

在应用程序中使用 SQL 时,不应将字符串中的值连接起来以避免 SqlInjection。有一个类SqlParameter,您可以参考这个问题以获取使用它的正确方法。

下面是使用SqlParameter和关闭SqlConnection的代码的修改版本

string command = string.Format(@"SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address FROM BC where {0} LIKE @value", combobox1.Text); 
DataTable dt;
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Seif-'Documents'Visual Studio 2013'Projects'BusinessCard'BusinessCard'BusinessCards.mdf;Integrated Security=True"))
{
    SqlCommand sc = new SqlCommand(command, conn);
    sc.Parameters.Add("@value", textBox1.Text);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    dt = new DataTable();
    sda.Fill(dt);
}
Search f2 = new Search(dt);
f2.Show();
您需要

通过类的实例来计算第二个形式。 查看我的 2 表单项目表格 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string results = form2.GetData();
        }
    }
}

表格 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops for from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }
    }
}

您可以通过以下两种方式中的任何一种来执行此操作

  1. 创建公共属性并为其赋值。
  2. 通过构造函数传递值并在搜索表单中设置它们

法典:

Search f2 = new Search(); 
f2.result = <<search result variable>>
f2.Show();
Search f2 = new Search(<<search result variable>>); 
f2.Show();

Results.cs类上,更改构造函数以包含数据:

public class Results
{
    private DataTable _ResultsTable;
    public Results(DataTable ResultsTable)
    {
        _ResultsTable;
    }
}

这意味着表单实例化将是:

Results resForm = new Results(dt);   

这前提是您永远不会在没有数据集的情况下加载Results表单。

或者,如果您不想强制预先声明它,则始终可以将其设置为结果中的属性.cs:

public DataTable ResultsTable { get; set; }

然后,您可以像访问任何其他属性一样访问它:

Results resForm = new Results();
// various lines of code from your example above
resForm.ResultTable = dt;

就其价值而言,我认为您在数据库中的查询中拥有的代码比您需要的要多。 我相信你的大部分button1_Click代码都可以替换为这样的东西:

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
    SqlConnection conn = new SqlConnection(@"<your connection string>");
    SqlCommand sc = new SqlCommand(string.Format(@"
        SELECT Name, Post, Company, Country, Email, Mobile, Tel1, Tel2, Fax, Address
        FROM BC
        where {0} like @VAL", comboBox1.Text), conn);
    sc.Parameters.AddWithValue("@VAL", textBox1.Text);
    SqlDataAdapter sda = new SqlDataAdapter(sc);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    var = (string)sc.ExecuteScalar();
    Search f2 = new Search();
    f2.Show();
}

使用参数不仅可以阻止 SQL 注入,还可以处理 textBox1 中的任何奇数文本,例如如果用户输入:

I think I'll have some cake

这会因为撇号而炸毁你的代码。

如果您添加更多搜索选项或将其应用于将来的表,它也更具可扩展性。