C# 在新的windows窗体C中查看搜索引擎结果#
我正在使用一个组合框,其中包括公司和国家的名称,因此用户可以从中选择,以指定他希望在文本框中搜索的表单,我需要以不同的表单(results.cs)查看我的搜索结果,并且我的搜索引擎处于打开状态(main.cs),我该怎么做C# 在新的windows窗体C中查看搜索引擎结果#,c#,sql,C#,Sql,我正在使用一个组合框,其中包括公司和国家的名称,因此用户可以从中选择,以指定他希望在文本框中搜索的表单,我需要以不同的表单(results.cs)查看我的搜索结果,并且我的搜索引擎处于打开状态(main.cs),我该怎么做 private void button1_Click(object sender, EventArgs e) { this.Hide(); if (comboBox1.Text == "Name") { String var; SqlCo
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();
}
}
您可以通过以下两种方法中的任意一种来实现
Search f2=新搜索();
f2.结果=
f2.Show();
搜索f2=新搜索();
f2.Show();
您可以通过以下两种方法中的任意一种来完成此操作
Search f2=新搜索();
f2.结果=
f2.Show();
搜索f2=新搜索();
f2.Show();
您需要通过类的实例校准第二个表单。请参阅我的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表单项目 表格一 表格二
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";
}
}
}
最常用的方法是在
Search
构造函数中传递DataTable
Search f2 = new Search(dt);
在搜索
表单中,您将拥有一个私人成员来持有该值
private DataTable _results;
public Search(DataTable table)
{
_results = table;
}
通过这种方式,您可以在搜索
在应用程序中使用SQL时,不应将值合并到字符串中以避免。这有一个类,你可以参考它来找到正确的使用方法
下面是使用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();
最常用的方法是在
Search
构造函数中传递DataTable
Search f2 = new Search(dt);
在搜索
表单中,您将拥有一个私人成员来持有该值
private DataTable _results;
public Search(DataTable table)
{
_results = table;
}
通过这种方式,您可以在搜索
在应用程序中使用SQL时,不应将值合并到字符串中以避免。这有一个类,你可以参考它来找到正确的使用方法
下面是使用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();
在
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;
值得一提的是,我认为在数据库中的查询中,代码比您需要的要多。我相信您的按钮1\u单击代码可以替换为以下内容:
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();
}
这会因为撇号而破坏你的代码
如果您添加更多搜索选项或将其应用于未来的表,它的可扩展性也更高。在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;
值得一提的是,我认为在数据库中的查询中,代码比您需要的要多。我相信您的按钮1\u单击代码可以替换为以下内容:
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();
}
这会因为撇号而破坏你的代码
如果您添加更多搜索选项或将其应用于未来的表,它的可扩展性也更高。为可读性重新格式化代码为记录重新格式化代码,我不是有意要窃取您的答案。。。这似乎与我在问题核心上所说的相同,尽管我有一些无关的评论。如果OP喜欢我和Sathik给出的答案,请把他标记为正确。为了记录在案,我不是故意要窃取你的答案。。。这似乎与我在问题核心上所说的相同,尽管我有一些无关的评论。如果OP喜欢我和Sathik提出的答案,请把他标记为正确。哇,我也偷了你的答案。。。哇,我也偷了你的答案。。。德拉特。