错误“必须声明变量@ studentnr”。我可以解决它吗? [英] Error"must declare variable @studentnr".how can I solve it?

查看:102
本文介绍了错误“必须声明变量@ studentnr”。我可以解决它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在我的程序中插入数据时遇到问题。这是我的代码:

 public void SaveData(object param)
{
string connstring = null;
connstring =数据源=(LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users \\user0909 \\Documents \\AtndanceStudents.mdf; Integrated Security = True ;连接超时= 30;
try
{
using(SqlConnection con = new SqlConnection(connstring))
{


string sql =Insert into RegisterStudent( SN,信噪比,FNAME,LNAME,学期)VALUES(@ SN,@ studentNr,@ FNAME,LNAME @,@学期);
con.Open();
using(SqlCommand cmd = new SqlCommand(sql,con))
{
cmd.Parameters.AddWithValue(@ SN,SqlDbType.Int).Value = sn;
cmd.Parameters.AddWithValue(@ sNr,SqlDbType.Int).Value = studentNr;
cmd.Parameters.AddWithValue(@ fName,SqlDbType.NVarChar).Value = fName;
cmd.Parameters.AddWithValue(@ lName,SqlDbType.NVarChar).Value = lName;
cmd.Parameters.AddWithValue(@ semester,SqlDbType.Int).Value = semester;
if(!string.IsNullOrEmpty(sql))
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

}

}
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message);
}
}





我在文本框中插入所有值,但是我得到了错误

必须声明变量@studentNr



这是我的表:

创建表[dbo]。[RegisterStudent] 

[SN] INT NOT NULL PRIMARY KEY,
[sNr] INT NOT NULL,
[fName] NVARCHAR(MAX)NOT NULL,
[lName] NVARCHAR(MAX)NOT NULL,
[semester] INT NULL





我尝试过:



这就是我的尝试:

 SqlParameter StudentNR = cmd.Parameters.AddWithValue(@ sNr,SqlDbType.Int); 

cmd.Parameters [@ sNr]。Value = studentNr;
if(StudentNR.Value == null)
{
StudentNR.Value = DBNull.Value;
}



我试过这种方法,但我仍然得到错误?可能是什么问题?提前谢谢!

解决方案

在SQL字符串中,您使用的是参数名@studentNr,但您定义的参数名为@sNr,它们必须匹配



 string sql =插入RegisterStudent(SN,sNr,fName,lName,semester)值(@ sn,@ sNr,@ fName,@ lName,@ semester); 


Hello,I have a problem in my program when I want to insert data into the db.This is my code:

public void SaveData(object param)
      {
          string connstring = null;
          connstring = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\user0909\\Documents\\AttendanceStudents.mdf;Integrated Security=True;Connect Timeout=30";
          try
          {
              using (SqlConnection con = new SqlConnection(connstring))
              {


                  string sql = "Insert into RegisterStudent(SN,sNr,fName,lName,semester)Values(@sn,@studentNr,@fName,@lName,@semester)";
                  con.Open();
                  using (SqlCommand cmd = new SqlCommand(sql, con))
                  {
                      cmd.Parameters.AddWithValue("@SN", SqlDbType.Int).Value = sn;
                      cmd.Parameters.AddWithValue("@sNr", SqlDbType.Int).Value = studentNr;
                      cmd.Parameters.AddWithValue("@fName", SqlDbType.NVarChar).Value = fName;
                      cmd.Parameters.AddWithValue("@lName", SqlDbType.NVarChar).Value = lName;
                      cmd.Parameters.AddWithValue("@semester", SqlDbType.Int).Value = semester;
                      if(!string.IsNullOrEmpty(sql))
                      {
                          cmd.CommandText = sql;
                          cmd.ExecuteNonQuery();

                      }

                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }



I insert all the values in the textboxes,but I get the error"

Must declare variable @studentNr"


This is my table:

CREATE TABLE [dbo].[RegisterStudent]
(
	[SN] INT NOT NULL PRIMARY KEY, 
    [sNr] INT NOT NULL, 
    [fName] NVARCHAR(MAX) NOT NULL, 
    [lName] NVARCHAR(MAX) NOT NULL, 
    [semester] INT NULL
)



What I have tried:

This is what I have tried:

SqlParameter StudentNR = cmd.Parameters.AddWithValue("@sNr", SqlDbType.Int);

             cmd.Parameters["@sNr"].Value = studentNr;
             if (StudentNR.Value == null)
             {
                 StudentNR.Value = DBNull.Value;
             }


I tried this approach,but I still get the error?What could be the problem?Thank you in advance!

解决方案

In the SQL string you are using a param name @studentNr but the param you are defining is called @sNr, they have to match

string sql = "Insert into RegisterStudent(SN,sNr,fName,lName,semester)Values(@sn,@sNr,@fName,@lName,@semester)";


这篇关于错误“必须声明变量@ studentnr”。我可以解决它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆