在C#中将记录插入表时无法将NULL值传递给参数
发布时间:2020-12-16 02:02:55 所属栏目:百科 来源:网络整理
导读:参见英文答案 The parameterized query expects the parameter which was not supplied????????????????????????????????????6个 我正在尝试构建一些查询并使用C#将包含7列的列表插入到SQL表中. 在我的列表中,我有几列的NULL值,我无法将它们传递给以下查询 s
参见英文答案 >
The parameterized query expects the parameter which was not supplied????????????????????????????????????6个
我正在尝试构建一些查询并使用C#将包含7列的列表插入到SQL表中. 在我的列表中,我有几列的NULL值,我无法将它们传递给以下查询 string strInsertListToTable = @"INSERT INTO ImpliedOutrightData (id,product,term,bid,offer,bidcp,offercp) VALUES(@id,@product,@term,@bid,@offer,@bidcp,@offercp)"; for (int i = 0; i < resultList.Count; i++) { SqlCommand cmdInsertList = new SqlCommand(strInsertListToTable,sqlcon); cmdInsertList.CommandType = CommandType.Text; cmdInsertList.Parameters.Clear(); cmdInsertList.Parameters.AddWithValue("@id",resultList[i].id); cmdInsertList.Parameters.AddWithValue("@product",resultList[i].product); cmdInsertList.Parameters.AddWithValue("@term",resultList[i].term); cmdInsertList.Parameters.AddWithValue("@bid",resultList[i].bid); cmdInsertList.Parameters.AddWithValue("@offer",resultList[i].offer); cmdInsertList.Parameters.AddWithValue("@bidcp",resultList[i].bidcp); cmdInsertList.Parameters.AddWithValue("@offercp",resultList[i].offercp); cmdInsertList.ExecuteNonQuery(); } 虽然上面的查询循环我得到错误 The parameterized query '(@id int,@product nvarchar(2),@term nvarchar(5),@bid float,@bidc' expects the parameter '@offercp',which was not supplied. 解决方法
当参数的值为null时,应将相应的值设置为DbNull.Value:
cmdInsertList.Parameters.AddWithValue( "@offercp",resultList[i].offercp == null ? (object)DbNull.Value : resultList[i].offercp ); 注意转换为对象 – 您需要这样,以便条件的两边评估为相同的类型. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容