我正在尝试设置一个简单的 c# 表单来搜索我们的一个 SQL 数据库。当我尝试调试时,我不断收到“SQLException was Unhandled”错误。我知道 SQL 语句有效,我可以在我的服务器上正常运行它。我认为这是我尝试从文本框中输入数据的方式的问题。有人可以给我一些指导吗?
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;
using System.Data.SqlClient;
namespace USER_Lookup
{
public partial class Search : Form
{
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection("Data Source=PC01; Initial Catalog=DB01; Integrated Security=TRUE");
SqlDataAdapter da = new SqlDataAdapter();
public Search()
{
InitializeComponent();
}
private void button_Search_Click(object sender, EventArgs e)
{
string badgeCode = textBox_badgeCode.Text.Trim();
da.SelectCommand = new SqlCommand
("SELECT db01.dbo.staff.lastname AS 'Last Name', db01.dbo.staff.firstname AS 'First Name', db01.dbo.staff.badgecode AS 'User ID', db01.dbo.staffrole.name AS 'Role' FROM db01.dbo.staff, db01.dbo.staffrole, db01.dbo.staff_staffrole WHERE db01.dbo.staff.badgecode =" + badgeCode + "AND db01.dbo.staff.id = db01.dbo.staff_staffrole.staff_id AND db01.dbo.staff_staffrole.staffrole_id = db01.dbo.staffrole.id", cs);
ds.Clear();
da.Fill(ds);
dg.DataSource = ds.Tables[0];
}
}
}