c# - 为什么我尝试插入时出现 "Must declare the scalar variable "@rollnum""错误?

标签 c# mysql ado.net

我给出的代码是--------

                SqlConnection cn = new SqlConnection(@"Data Source=CHROMA\SQLEXPRESS;Initial Catalog=marksheet;Integrated Security=True"); 
                cn.Open();

                if (cn != null)
                {

                    Console.WriteLine("Enter roll number");
                   string rollnum =Console.ReadLine();

                    Console.WriteLine("Enter name");
                    string name = Console.ReadLine();

                    Console.WriteLine("Enter gender");
                    string gen = Console.ReadLine();

                    Console.WriteLine("Enter DOB");
                    string DOB = Console.ReadLine();

                    Console.WriteLine("Enter father name");
                    string father = Console.ReadLine();

                    Console.WriteLine("Enter course");
                    string course = Console.ReadLine();

                    Console.WriteLine("Enter address");
                    string add = Console.ReadLine();

                    Console.WriteLine("Enter city");
                    string city = Console.ReadLine();

                    Console.WriteLine("Enter state");
                    string state = Console.ReadLine();

                    Console.WriteLine("Enter phone");
                    string phone = Console.ReadLine();
        SqlCommand cmd = new SqlCommand("insert into student (rollno,name,gender,dob,fname,course,address,city,state,phone) values(@rollnum,@name,@gen,@DOB,@father,@course,@add,@city,@state,@phone,)",cn);

                   int row= cmd.ExecuteNonQuery();
                    if(row >0)

                    Console.WriteLine("Record inserted...");
                    cn.Close();
                    cn.Dispose();
                }
                else
                    Console.WriteLine("please open connection first!!");

最佳答案

您应该在创建命令之后和执行命令之前绑定(bind)您的参数。 例如: cmd.Parameters.AddWithValue("@rollnum", rollnum); (对所有其他参数执行相同操作)

关于c# - 为什么我尝试插入时出现 "Must declare the scalar variable "@rollnum""错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28851075/

相关文章:

c# - PrivateFontCollection.AddMemoryFont 是否适用于 WOFF 文件?

c# - 检查值是否存在或为空

mysql - 如何获取mysql中查询所使用的内存大小?

mysql - 如何向 MySQL 数据字段添加正则表达式?

c# - SqlCommand 最大参数异常在 2099 个参数

c# - ExecuteReader 需要一个打开且可用的连接。连接的当前状态是 Connecting

c# - ASP.Net 缓存问题/疑问。在何处以及如何缓存。 |服装 ADO.Net 代码

c# - 如何将多个参数传递给一个方法?

c# - fatal error 导致超时 -mysql

mysql - 如何组合查询结果?