我对 Windows 应用程序编程比较陌生。我已经开始使用 C# 并且需要一些帮助。我有一个带有客户表(填充数据)的 SQL 数据库我已经创建了一个客户添加页面(成功添加新客户并验证它不是冗余客户。我现在正在尝试创建一个基于 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 个文本框。我也想我还不能在这里发布图片(没有足够的代表)