我的代码是--------

                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!!");

最佳答案

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

关于c# - 为什么在尝试插入时出现“必须声明标量变量“@rollnum””错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28851075/

10-08 23:15