SqlException 必须声明标量变量"@ID"



我收到一个错误

SqlException必须声明标量变量"ID";

这是代码。我已经尝试过使用不同的方法,但仍然是相同的

try
{
AdminPanel n = new AdminPanel();
if (textBox1.Text == string.Empty)
{
MessageBox.Show("Please enter Student ID");
}
else
{
string commstring = "SELECT * FROM student WHERE studentno = @ID";
using (SqlConnection connect = new SqlConnection(constring))
using (SqlCommand comm = new SqlCommand(commstring, connect))
{
comm.Connection.Open();
comm.Parameters.AddWithValue("@ID", textBox1.Text);
SqlDataReader read = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (read.Read() == false)
{
MessageBox.Show("The student ID you've entered doesn't have a library card yet.", "No Record");
}
else
{
checkIssue();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

我认为您缺少studentno的数据类型,代码应该是这样的:

(如果studentno为int类型:(

comm.Parameters.AddWithValue("@ID",DbType.Int32,textBox1.Text(;

(如果studentno是字符串类型:(

comm.Parameters.AddWithValue("@ID",DbType.String,textBox1.Text(;

相关内容

  • 没有找到相关文章

最新更新