本文介绍了system.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的未处理异常附加信息:必须声明标量变量“@ 8”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人可以帮我解决这个错误,我一直在努力,没有运气。我正在尝试使用C#Professional 2013在数据库中注册用户详细信息。我收到此错误:system.Data.dll中发生类型'System.Data.SqlClient.SqlException'的未处理异常附加信息:必须声明标量变量@ 8。 这是我的代码:我似乎根本不保存图像。 private void btnRegister_Click( object sender,EventArgs e) { if (txtUserName.Text == ) { MessageBox.Show( 请输入用户名, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtUserName.Focus(); return ; } if (cmbUserType.Text == ) { MessageBox.Show( Please选择用户类型, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtUserName.Focus(); return ; } 如果(txtPassword.Text == ) { MessageBox.Show( Please输入密码, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtPassword.Focus(); return ; } if (txtConfirmPassword.Text!= txtPassword.Text) { MessageBox.Show( 密码与, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtConfirmPassword.Focus(); return ; } if (txtFirstName.Text == ) { MessageBox.Show( Please输入名称, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtFirstName.Focus(); return ; } 如果(txtPhoneNo.Text == ) { MessageBox.Show( Please输入联系号码, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error) ; txtPhoneNo.Focus(); return ; } if (txtEmail.Text == ) { MessageBox.Show( Please输入电子邮件, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtEmail.Focus(); return ; } // 尝试 // { con = new SqlConnection(cs。 DBConn); con.Open(); string ct = 从注册中选择用户名其中Username = @ find; cmd = new SqlCommand(ct); cmd.Connection = con; cmd.Parameters.Add( new SqlParameter( @find,System.Data.SqlDbType.VarChar, 30 , 用户名)); cmd.Parameters [ @ find]。Value = txtUserName.Text; rdr = cmd.ExecuteReader(); if (rdr.Read()) { MessageBox.Show( 用户名已存在, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); txtUserName.Text = ; txtUserName.Focus(); if ((rdr!= null )) { rdr.Close(); } return ; } con = new SqlConnection(cs.DBConn); con.Open(); string cb = 插入注册(用户名,用户密码,姓名,姓氏,电话号码,电子邮件,DateOfBirth,用户类型,图片)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6,@ d7,@ 8,@ 9 ); cmd = new SqlCommand(cb); cmd.Connection = con; cmd.Parameters.Add( @ d1,System.Data .SqlDbType.VarChar); cmd.Parameters.Add( @ d2,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d3,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d4,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d5,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d6,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d7,System.Data.SqlDbType.VarChar ); cmd.Parameters.Add( @ d8,System.Data.SqlDbType.VarChar ); // cmd.Parameters.Add(new SqlParameter(@ d9,System.Data.SqlDbType。图像,32,图片)); MemoryStream MemStream = new MemoryStream(); 字节 [] DataPic_Update = null ; this .pictureImage.Image.Save(MemStream,ImageFormat.Jpeg); DataPic_Update = MemStream.GetBuffer(); MemStream.Read(DataPic_Update, 0 ,DataPic_Update.Length); cmd.Parameters [ @ d1]。Value = txtUserName.Text.Trim(); cmd.Parameters [ @ d2]。Value = txtPassword.Text; cmd.Parameters [ @ d3]。Value = txtFirstName.Text; cmd.Parameters [ @ d4]。Value = txtLastName.Text; cmd.Parameters [ @ d5]。Value = txtPhoneNo.Text; cmd.Parameters [ @ d6]。Value = txtEmail.Text; cmd.Parameters [ @ d7]。Value = dateDOB.Value; cmd.Parameters [ @ d8]。Value = cmbUserType.Text; // 图片内容 SqlParameter photo = new SqlParameter( @ d9,SqlDbType.Image); photo.Value = DataPic_Update; cmd.Parameters.Add(照片); cmd.ExecuteReader(); // cmd.ExecuteNonQuery(); con。关(); con = new SqlConnection(cs.DBConn); con.Open(); string cb1 = 插入用户(用户名,用户密码,用户类型)VALUES(@ d1,@ d2,@ d3); cmd = new SqlCommand(cb1); cmd.Connection = con; cmd.Parameters.Add( new SqlParameter( @ d1,System.Data.SqlDbType.VarChar, 30 , 用户名)); cmd.Parameters.Add( new SqlParameter( @ d2,System.Data.SqlDbType.VarChar, 30 , Userpassword)); cmd.Parameters.Add( new SqlParameter( @ d3,System.Data.SqlDbType.VarChar, 15 , UserType)); cmd.Parameters [ @ d1]。Value = txtUserName。 Text.Trim(); cmd.Parameters [ @ d2]。Value = txtPassword.Text; cmd.Parameters [ @ d3]。Value = cmbUserType。文本; cmd.ExecuteReader(); con.Close(); MessageBox.Show( 已成功注册, 用户,MessageBoxButtons.OK,MessageBoxIcon.Information); Autocomplete(); btnRegister.Enabled = false ; // } // catch(exception ex) // { // MessageBox.Show(ex.Message) ,Error,MessageBoxButtons.OK,MessageBoxIcon.Error); // } } 非常感谢解决方案 你的参数名称不匹配... 用@ d8和@ d9替换@ 8和@ 9插入查询... 像这样更改你的代码 string cb = 插入注册(使用rname,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6,@ d7,@ d8,@ d9); 问题是,在查询中,您声明的参数名称与您添加到命令中的参数名称不同。 string cb = 插入注册(用户名,用户密码,姓名,姓氏,电话号码,电子邮件,DateOfBirth,用户类型,图片)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6,@ d7, @ 8,@ 9 ); // ................ ... cmd.Parameters.Add( @ d8 ,System.Data.SqlDbType.VarChar); // ................ SqlParameter photo = new SqlParameter( @ d9 ,SqlDbType.Image); 标记参数名称不同。 因此,您需要更改查询以反映与添加的参数相同的名称。 string cb = 插入注册(用户名,用户密码,姓名,姓氏, PhoneNumber,Email,DateOfBirth,UserType,Picture)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6,@ d7, @ d8,@ d9 ) Could some one help me to resolve this error please, I have been trying without luck. I am trying to register user details in database using C# Professional 2013. I ma getting this error: An unhandled exception of the type 'System.Data.SqlClient.SqlException' occurred in system.Data.dll Additional information: Must declare the Scalar variable "@8".Here is my code: I also seem not to save the image at all.private void btnRegister_Click(object sender, EventArgs e) { if (txtUserName.Text == "") { MessageBox.Show("Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtUserName.Focus(); return; } if (cmbUserType.Text == "") { MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtUserName.Focus(); return; } if (txtPassword.Text == "") { MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPassword.Focus(); return; } if (txtConfirmPassword.Text != txtPassword.Text) { MessageBox.Show("The password does not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtConfirmPassword.Focus(); return; } if (txtFirstName.Text == "") { MessageBox.Show("Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtFirstName.Focus(); return; } if (txtPhoneNo.Text == "") { MessageBox.Show("Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPhoneNo.Focus(); return; } if (txtEmail.Text == "") { MessageBox.Show("Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtEmail.Focus(); return; } // try // { con = new SqlConnection(cs.DBConn); con.Open(); string ct = "select username from Registration where Username=@find"; cmd = new SqlCommand(ct); cmd.Connection = con; cmd.Parameters.Add(new SqlParameter("@find", System.Data.SqlDbType.VarChar, 30, "Username")); cmd.Parameters["@find"].Value = txtUserName.Text; rdr = cmd.ExecuteReader(); if (rdr.Read()) { MessageBox.Show("Username Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtUserName.Text = ""; txtUserName.Focus(); if ((rdr != null)) { rdr.Close(); } return; } con = new SqlConnection(cs.DBConn); con.Open(); string cb = "insert into Registration(Username,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@8,@9)"; cmd = new SqlCommand(cb); cmd.Connection = con; cmd.Parameters.Add("@d1", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d2", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d3", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d4", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d5", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d6", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d7", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@d8", System.Data.SqlDbType.VarChar); //cmd.Parameters.Add(new SqlParameter("@d9", System.Data.SqlDbType.Image,32, "picture")); MemoryStream MemStream = new MemoryStream(); Byte[] DataPic_Update = null; this.pictureImage.Image.Save(MemStream, ImageFormat.Jpeg); DataPic_Update = MemStream.GetBuffer(); MemStream.Read(DataPic_Update, 0, DataPic_Update.Length); cmd.Parameters["@d1"].Value = txtUserName.Text.Trim(); cmd.Parameters["@d2"].Value = txtPassword.Text; cmd.Parameters["@d3"].Value = txtFirstName.Text; cmd.Parameters["@d4"].Value = txtLastName.Text; cmd.Parameters["@d5"].Value = txtPhoneNo.Text; cmd.Parameters["@d6"].Value = txtEmail.Text; cmd.Parameters["@d7"].Value = dateDOB.Value; cmd.Parameters["@d8"].Value = cmbUserType.Text; // image content SqlParameter photo = new SqlParameter("@d9", SqlDbType.Image); photo.Value = DataPic_Update; cmd.Parameters.Add(photo); cmd.ExecuteReader(); // cmd.ExecuteNonQuery(); con.Close(); con = new SqlConnection(cs.DBConn); con.Open(); string cb1 = "insert into users(Username,Userpassword,UserType) VALUES (@d1,@d2,@d3)"; cmd = new SqlCommand(cb1); cmd.Connection = con; cmd.Parameters.Add(new SqlParameter("@d1", System.Data.SqlDbType.VarChar, 30, "Username")); cmd.Parameters.Add(new SqlParameter("@d2", System.Data.SqlDbType.VarChar, 30, "Userpassword")); cmd.Parameters.Add(new SqlParameter("@d3", System.Data.SqlDbType.VarChar, 15, "UserType")); cmd.Parameters["@d1"].Value = txtUserName.Text.Trim(); cmd.Parameters["@d2"].Value = txtPassword.Text; cmd.Parameters["@d3"].Value = cmbUserType.Text; cmd.ExecuteReader(); con.Close(); MessageBox.Show("Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information); Autocomplete(); btnRegister.Enabled = false; //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // } }Thank you very much 解决方案 your parameter names are mismatch...replace @8 and @9 in youe insert query with @d8 and @d9...change your code like thisstring cb = "insert into Registration(Username,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9)";The problem is, in the query you have declared the parameter name different from that you have added to the command.string cb = "insert into Registration(Username,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@8,@9)";// ...................cmd.Parameters.Add("@d8", System.Data.SqlDbType.VarChar);//................SqlParameter photo = new SqlParameter("@d9", SqlDbType.Image);Mark that the Parameter names are different.So, you need to change the query to reflect the same name as the parameters added.string cb = "insert into Registration(Username,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9)"; 这篇关于system.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的未处理异常附加信息:必须声明标量变量“@ 8”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 02:47