C# SQL 搜索结果变成 string/textbox
C# SQL Search result into a string/textbox
我正在尝试创建注册表单。我想进行验证检查,如果文本框中的 ID 已经存在,它将显示错误消息。但是,每当我尝试搜索 ID 并将其放入字符串时,我都会收到错误消息
An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
这是我的代码:
public SignUpForm()
{
InitializeComponent();
}
// Connection String
SqlCeConnection connect = new SqlCeConnection("Data Source= Accounts.sdf;Persist Security Info=false;");
private void btnRegister_Click(object sender, EventArgs e) {
String verifyID = "";
connect.Open();
using (SqlCeCommand cmd = new SqlCeCommand("SELECT Student ID FROM Users WHERE Student ID = @ID", connect))
{
cmd.Parameters.AddWithValue("@ID", txtID.Text);
using (SqlCeDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
verifyID = (string)reader["Student ID"];
}
}
}
txtTEMP.Text = verifyID;
可能错误出在您的 SQL 语句中。考虑将其更改为:
SELECT StudentID FROM Users WHERE StudentID = @ID
你的SQLSELECT [Student ID] FROM Users WHERE Student ID = @ID
好像不对。应该是
SELECT [Student ID] FROM Users WHERE [Student ID] = @ID
我正在尝试创建注册表单。我想进行验证检查,如果文本框中的 ID 已经存在,它将显示错误消息。但是,每当我尝试搜索 ID 并将其放入字符串时,我都会收到错误消息
An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
这是我的代码:
public SignUpForm()
{
InitializeComponent();
}
// Connection String
SqlCeConnection connect = new SqlCeConnection("Data Source= Accounts.sdf;Persist Security Info=false;");
private void btnRegister_Click(object sender, EventArgs e) {
String verifyID = "";
connect.Open();
using (SqlCeCommand cmd = new SqlCeCommand("SELECT Student ID FROM Users WHERE Student ID = @ID", connect))
{
cmd.Parameters.AddWithValue("@ID", txtID.Text);
using (SqlCeDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
verifyID = (string)reader["Student ID"];
}
}
}
txtTEMP.Text = verifyID;
可能错误出在您的 SQL 语句中。考虑将其更改为:
SELECT StudentID FROM Users WHERE StudentID = @ID
你的SQLSELECT [Student ID] FROM Users WHERE Student ID = @ID
好像不对。应该是
SELECT [Student ID] FROM Users WHERE [Student ID] = @ID