如果前一个参数为空,则参数没有默认值?

Parameter has no default value if previous param is null?(如果前一个参数为空,则参数没有默认值?)
本文介绍了如果前一个参数为空,则参数没有默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have this query which seems to work unless I try to pass a null value in a parameter:

using (OleDbCommand com = new OleDbCommand("INSERT INTO [GROUP] ([Group Number], [Group Name], [Address 1], " +
                        " [Address 2], [City State Zip], [Phone], [Carrier Code], [Retail Plan Num 1], [Retail Plan Num 2], " +
                        " [Retail Plan Num 3], [Retail Plan Num 4], [Retail Plan Num 5], [Plan Start Date 1], [Plan Start Date 2], " +
                        " [Plan Start Date 3], [Plan Start Date 4], [Plan Start Date 5]" +
                        ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", con))
                    {
                        com.Parameters.Add("@p0", OleDbType.Char, 255).Value = values[0];
                        com.Parameters.Add("@p1", OleDbType.Char, 255).Value = values[1];
                        com.Parameters.Add("@p2", OleDbType.Char, 255).Value = values[2];
                        com.Parameters.Add("@p3", OleDbType.Char, 255).Value = values[3];
                        com.Parameters.Add("@p4", OleDbType.Char, 255).Value = values[4];
                        com.Parameters.Add("@p5", OleDbType.Char, 255).Value = values[5];
                        com.Parameters.Add("@p6", OleDbType.Char, 255).Value = values[6];

                        string[] values7 = values[7].Split('	');                               
                        com.Parameters.Add("@p7", OleDbType.Char, 255).Value = values7[0];
                        com.Parameters.Add("@p8", OleDbType.Char, 255).Value = values7[1];
                        com.Parameters.Add("@p9", OleDbType.Char, 255).Value = values7[2];
                        com.Parameters.Add("@p10", OleDbType.Char, 255).Value = values7[3];
                        com.Parameters.Add("@p11", OleDbType.Char, 255).Value = values7[4];

                        com.Parameters.Add("@p12", OleDbType.Date, 255).Value = "1/1/1999";
                        com.Parameters.Add("@p13", OleDbType.Date, 255).Value = null;   
                        com.Parameters.Add("@p14", OleDbType.Date, 255).Value = "1/1/1999";
                        com.Parameters.Add("@p15", OleDbType.Date, 255).Value = "1/1/1999";
                        com.Parameters.Add("@p16", OleDbType.Date, 255).Value = "1/1/1999";

                        com.ExecuteNonQuery();
                    }

This will return an error Parameter ?_14 has no default value. If I change my code above so the p12 parameter is null, then the error says Parameter ?_13 has no default value.

What's going on here?

解决方案

Parameters with a .Value of null are not passed. At all.

You need to pass DBNull.Value instead to pass a semantic null. For example:

com.Parameters.Add("@p7", OleDbType.Char, 255).Value =
         ((object)values7[0]) ?? DBNull.Value;

(etc)

And yes: I agree that this is ridiculous

这篇关于如果前一个参数为空,则参数没有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)