我对Windows应用程序编程比较陌生。我已经开始使用C#并需要一些帮助。我有一个带有Customers表的SQL数据库(填充数据)我创建了一个Customer Add页面(成功添加新客户并验证它不是一个冗余客户。我现在正在尝试创建一个基于TB字段的客户搜索页面您可以通过名字,姓氏,电话号码或电子邮件地址进行搜索,这些字段中的任何一个或全部将查询数据库表并将数据返回到同一表单下面的另一个表单或数据网格。我有的教程到目前为止发现自动从数据库中提取数据并将其输入TB字段,这不是我想要的。 如第一张图片所示
SQl表如下
customerID int PK,
trackingID int FK,
barterID int FK,
firstName varchar(50),
lastName varchar(50),
address1 varchar(75),
address2 varchar(75),
city varchar(50),
st varchar(25),
zip varchar(10),
contactNumber varchar(15),
contactNumber2 varchar(15),
email varchar(50),
username varchar(50),
pwd varchar(50),
customerSince datetime2(7),
lastVisit datetime2(7),
company varchar(50)
我为查询输入的代码
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 Trex
{
public partial class CustomerSearch : Form
{
public CustomerSearch()
{
InitializeComponent();
}
private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.customersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.trexDataSet1);
}
private void customersBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.customersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.trexDataSet1);
}
private void customersBindingNavigatorSaveItem_Click_2(object sender, EventArgs e)
{
this.Validate();
this.customersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.trexDataSet1);
}
private void CustomerSearch_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'trexDataSet1.customers' table. You can move, or remove it, as needed.
this.customersTableAdapter.Fill(this.trexDataSet1.customers);
}
private void btnClear_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i] is TextBox)
{
this.Controls[i].Text = "";
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
private void firstNameTextBox_TextChanged(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=VISIONSWS-PC;Initial Catalog=Trex;Persist Security Info=True;User ID=sa;Password=password");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT * FROM customers WHERE (firstName = @tbFirstName) and (lastName = @tbLastName)";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.EndExecuteReader();
sqlConnection1.Close();
}
}
}
我只尝试了前2个文本框,另外2个我确定在输入正确的代码后可以很容易地添加它以及其他搜索条件。我想我也不能在这里发布图片(不够代表)