参数化查询‘(@UserName nvarchar(10),@NickName nvarchra(50)’ 需要参数 @NickName,但未提供该参数

在尝试向数据库表添加数据时,如果遇到因为空值引发的错误,可以采取两种方法进行处理。方法一是在实体类赋值时使用三目运算确保空值被转换为空字符串。方法二是在数据访问层(DAL)中,循环遍历SqlParameter数组,将空值替换为DBNull.Value。这两种方法都能有效避免因为空值导致的参数化查询失败。

我们往数据库表中添加一行数据的时候,数据库允许某一列为空,但是实际案例操作添加的时候,会遇到一些问题,比如下图。这样,我们就需要做一些处理。

这个两种修改方式

第一种:

在方法体中,实体类赋值时修改。用三目运算。

 UserInfo userInfo = new UserInfo();

userInfo.NickName = string.IsNullOrEmpty(register.txtNickName) == true ? "" : register.txtNickName.ToLower().Trim();

第二种:

在DAL(数据链路层),

 SqlParameter[] pars = { 
                                    new SqlParameter("@UserName",userInfo.UserName),
                                    new SqlParameter("@NickName",userInfo.NickName),
                                    new SqlParameter("@Password",userInfo.Password.),
                                    new SqlParameter("@Mail",userInfo.Mail),
                                    new SqlParameter("@Address",userInfo.Address),
                                    new SqlParameter("@Phone",userInfo.Phone)
                                  };
            foreach (SqlParameter item in pars)
            {
                if (item.Value==null)
                {
                    item.Value = DBNull.Value;
                }
            }

第一种直接用空字符串。第二种,循环遍历。对传入的数据作空值的处理,一旦其为空,就设置为DBNull.value.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 图书管理系统 { public partial class Register : Form { public static List enders = new List(); public static ListmangerEnders = new List(); public Register() { InitializeComponent(); enders.Add(new Ender { admin = “asdasd”, codes = “123123” }); mangerEnders.Add(new MangerEnder { Mangeradmin = “asdqwe”, Mangercodes = “111111”}); } private void button1_Click(object sender, EventArgs e) { bool flage=false; for (int i = 0; i <mangerEnders.Count; i++) { if ((textBox1.Text == mangerEnders[i].Mangeradmin && textBox2.Text == mangerEnders[i].Mangercodes) || (textBox1.Text == enders[i].admin && textBox2.Text == enders[i].codes)) { new Form1().Show(); this.Close(); flage=false; } } if (flage!=false) { MessageBox.Show("账号或者密码错误,请重新输入", "登录", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button2_Click(object sender, EventArgs e) { new Login().Show(); } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _04Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string userName = "全款5"; string passWord = "999999"; string nickName = "赃款"; string insertSQL = $"insert into Table_1 values('{userName}','{passWord}','{nickName}')"; int count = EditData(insertSQL); if (count > 0) { Console.WriteLine("新增成功"); } else if (count <= 0) { Console.WriteLine("新增失败"); } string selectSQL = "select * from Table_1"; DataTable aa = SelectData(selectSQL); foreach (DataRow item in aa.Rows) { Console.WriteLine($" {item["UserName"]} ---- {item["PassWord"]} --- {item["NickName"]}"); } } public static int EditData(string sql) //增 删的方法 { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Server=localhost\\SQLEXPRESS;Database=Text2;Trusted_Connection=true"; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); int count = 0; try { count = cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine("----------------------" + ex); count = 0; } conn.Close(); return count; } public static DataTable SelectData(string sql) { SqlConnection conn = new SqlConnection(); //通过指定属性的方式直接赋值 //SQLEXPRESS //要连接的数据 要操作的数据库文件 采用window账号 密码的方式 conn.ConnectionString = "Server=localhost\\SQLEXPRESS;Database=Text2;Trusted_Connection=true"; //最后一个参数表示默认的连接方式 conn.Open(); //打开数据库 //写执行的命令 SqlCommand cmd = new SqlCommand(sql, conn); //指定一下是由哪个数据库执行的哪个命令 //把语句提交之后,得到一个返回的结果 SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; //表格 DataSet dataSet = new DataSet(); adapter.Fill(dataSet); DataTable table = dataSet.Tables[0]; //搞个变量接收一下最终的返回信息 conn.Close(); //关闭数据库 return table; } } } 将数据库带入到登录代码
最新发布
09-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值