C#运行异常 Message=必须声明标量变量 "@rdID

问题遇到的现象和发生背景

图书管理系统运行以后尝试登录报错

img

img

img

img

问题相关代码,请勿粘贴截图
//登录按钮
private void btnLogin_Click(object sender, EventArgs e)
        {
            loginTimes++;
            int rdID;
            rdID = Convert.ToInt32(txtUserID.Text.Trim());
            reader = ReaderAdmin.GetReader(rdID);

            if (reader == null)
            {
                txtUserID.Focus();
                lblReaderInfo.Text = "登录信息:查无此人..{" + loginTimes.ToString() + "}";
            }
            else 
            {
            if(reader.rdPwd == txtUserPwd.Text)
                {
                    this.DialogResult = DialogResult.OK;

                }
            else
                {
                    txtUserPwd.Text = "";
                    txtUserPwd.Focus();
                    lblReaderInfo.Text = "登录信息:..密码错误!{" + lblReaderInfo.ToString() + "}";
                }
            }
        }

//异常
 public static DataTable GetDataTable(string sql, SqlParameter[] parameters, string TableName)
        {
            DataTable dt = null;
            try
            {
                OpenConn();
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (parameters != null)
                {
                    foreach (SqlParameter parameter in parameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, TableName);
                dt = ds.Tables[0];
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                CloseConn();
            }
            return dt;
        }
运行结果及报错内容
System.Exception
  HResult=0x80131500
  Message=必须声明标量变量 "@rdID"。
  Source=BookManage.DAL
  StackTrace:
   at BookManage.DAL.SqlHelper.GetDataTable(String sql, SqlParameter[] parameters, String TableName) in F:\作业\《WINDOWS程序设计课程设计》\BookManage.DAL\SqlHelper.cs:line 253
   at BookManage.DAL.ReaderDAL.GetDRByID(Int32 rdID) in F:\作业\《WINDOWS程序设计课程设计》\BookManage.DAL\ReaderDAL.cs:line 161
   at BookManage.DAL.ReaderDAL.GetObjectByID(Int32 rdID) in F:\作业\《WINDOWS程序设计课程设计》\BookManage.DAL\ReaderDAL.cs:line 175
   at BookManage.BLL.ReaderAdmin.GetReader(Int32 rdID) in F:\作业\《WINDOWS程序设计课程设计》\BookManage.BLL\ReaderAdmin.cs:line 21
   at BookManage.frmLogin.btnLogin_Click(Object sender, EventArgs e) in F:\作业\《WINDOWS程序设计课程设计》\BookManage\frmLogin.cs:line 34
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at BookManage.Program.Main() in F:\作业\《WINDOWS程序设计课程设计》\BookManage\Program.cs:line 23

我想要达到的结果

正常应该登录成功或显示查无此人或密码错误现在是出现异常我找了一天找不到哪里有问题该怎么解决啊

GetDataTable怎么调用的,看错误提示是sql语句包含@rdID这个参数,但是传入的parameters并没有@rdID参数,注意参数名称要加上@
示例

            var p = new SqlParameter("@rdID", SqlDbType.Int, 123);

img


有其他问题可以继续交流~

第三张图片里面,没有传递这个rdID,你再调用的时候就没有找到这个,就异常了。GetReader(int rdID,int rdType,int rdDept,int rdName);