查看搜索引擎结果以新的窗体形式C#

时间:2016-02-20 12:23:08

标签: c# sql

我正在使用包含名称公司和国家/地区的组合框,因此用户可以从中选择指定要在文本框中搜索的表单,我需要以不同的形式查看我的搜索结果(results.cs)和我的搜索引擎在(main.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();

  }
}

4 个答案:

答案 0 :(得分:1)

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

  1. 创建公共属性并为其分配值。
  2. 通过构造函数传递值并在搜索表单中设置
  3. 代码:

    box

答案 1 :(得分:1)

您需要通过类的实例来校准第二个表单。看我的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";
        }

    }
}

答案 2 :(得分:1)

最常见的方法是在DataTable构造函数中传递Search

Search f2 = new Search(dt);

Search表单中,您将拥有一个私有成员来保存该值。

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

这样您就可以在_results

中的任何地方使用Search

在应用程序中使用SQL时,不应将字符串中的值连接起来以避免SqlInjection。有一个课程SqlParameter,你可以参考这个question以获得正确的使用方法。

以下是使用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();

答案 3 :(得分:0)

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

public class Results
{
    private DataTable _ResultsTable;

    public Results(DataTable ResultsTable)
    {
        _ResultsTable;
    }
}

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

Results resForm = new Results(dt);   

这预先假定您在没有数据集的情况下永远不会加载Results表单。

或者,如果您不想强制它被预先声明,您可以随时将其作为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

由于撇号,会破坏你的代码。

它还具有更高的可扩展性,您可以添加更多搜索选项或将其应用于未来的表格。