Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Sql ADO.Net DataColumn添加可空Guid列时出错_C#_Ado.net - Fatal编程技术网

C#Sql ADO.Net DataColumn添加可空Guid列时出错

C#Sql ADO.Net DataColumn添加可空Guid列时出错,c#,ado.net,C#,Ado.net,我试图从C#调用sql存储过程。我有以下代码来创建DataColumn。但在添加可为空的Guid和日期时间类型时,在创建数据表时出错 DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(nameof(LearnerEntity.FirstName), typeof(string)), new Dat

我试图从C#调用sql存储过程。我有以下代码来创建DataColumn。但在添加可为空的Guid和日期时间类型时,在创建数据表时出错

DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[]
        {
            new DataColumn(nameof(LearnerEntity.FirstName), typeof(string)),
            new DataColumn(nameof(LearnerEntity.DateOfBirth), typeof(DateTime?)),
            new DataColumn(nameof(LearnerEmployerEntity.SectorId), typeof(Guid?)),
            new DataColumn(nameof(LearnerEntity.EPortfolioId), typeof(int?))
        });
        dt.Rows.Add(
            learnerEntity.FirstName,
            learnerEntity.DateOfBirth ?? SqlDateTime.Null,
            learnerEntity.Employer?.SectorId ?? SqlGuid.Null.Value,
            learnerEntity.EPortfolioId ?? SqlInt32.Null.Value); 
错误是:

DataSet does not support System.Nullable<>. 

感谢您,原始ADO.Net不理解或不使用可空类型(它早于可空类型)。相反,您必须设置(或不设置)每个数据列上的
AllowDBNull
属性


我真的很想看到一个假想的“ADO.Net v2”,它摒弃了DBNull,取而代之的是可空类型(DataRow和DataReader也实现了相同的IDataRecord接口,但这是另一个故事),但我发现我们不太可能看到这一点。

必须用
DBNull.Value
对其进行初始化。但是,由于在可为空的类型之间没有隐式转换,您将生成难看的代码。最好使用支持nullables的扩展方法:

DataRow row = dt.Rows.Add();
row.SetField<string>(nameof(LearnerEntity.FirstName), LearnerEntity.FirstName);
row.SetField<DateTime?>(nameof(LearnerEntity.DateOfBirth), LearnerEntity.DateOfBirth);
row.SetField<Guid?>(nameof(LearnerEmployerEntity.SectorId), learnerEntity.Employer?.SectorId);
row.SetField<int?>(nameof(LearnerEntity.EPortfolioId), LearnerEntity.EPortfolioId);
DataRow row=dt.Rows.Add();
row.SetField(nameof(LearnerEntity.FirstName),LearnerEntity.FirstName);
row.SetField(名称(LearnerEntity.DateOfBirth),LearnerEntity.DateOfBirth);
行.设置字段(名称(learnerEmployeerEntity.SectorId),learnerEntity.Employer?.SectorId);
row.SetField(名称(LearnerEntity.EPortfolioId),LearnerEntity.EPortfolioId);

您是否尝试用谷歌搜索错误?问题是
typeof(Guid?
typeof(DateTime?
)。默认情况下,列已经可以为空。使用
typeof(Guid)
typeof(DateTime)
typeof(int)
而不是在设置值时获取错误。“数据为Null。无法对Null值调用此方法或属性。”请使用
DBNull.Value
或仅使用
Null
。DataTable具有.NET类型,如string、int、DateTime、Guid,而不是特定于povider的类型设置AllowbNull时,我得到的是“数据为Null。不能对Null值调用此方法或属性。”errorWow。太好了。它起作用了
DataRow row = dt.Rows.Add();
row.SetField<string>(nameof(LearnerEntity.FirstName), LearnerEntity.FirstName);
row.SetField<DateTime?>(nameof(LearnerEntity.DateOfBirth), LearnerEntity.DateOfBirth);
row.SetField<Guid?>(nameof(LearnerEmployerEntity.SectorId), learnerEntity.Employer?.SectorId);
row.SetField<int?>(nameof(LearnerEntity.EPortfolioId), LearnerEntity.EPortfolioId);