简体   繁体   English

如何在SQL Server表中插入包含Null值的DataTable?

[英]How to Insert a DataTable containing Null values in to Sql Server Table?

I have a DataTable which also contains some Null values such as 我有一个DataTable,其中也包含一些Null值,例如

   ID             CustomerID           CompanyID
 -------        --------------       --------------
   1                  2                  Null
   2                 Null                1
   3                  5                  4

I want to insert this DataTable into a Table. 我想将此DataTable插入表中。 In SQL Server 2005, I have one Table called "JobDetails" which has three columns "JobOrderID", "CustomerID" and "CompanyID". 在SQL Server 2005中,我有一个名为“ JobDetails”的表,该表具有三列“ JobOrderID”,“ CustomerID”和“ CompanyID”。 So the above DataTable to be inserted into the "JobDetails" table like follows: 因此,将上面的DataTable插入“ JobDetails”表中,如下所示:

   JobOrderID      CustomerID          CompanyID
  ------------    ------------        ------------
       1               2                Null
       2              Null                1
       3               5                  4

How to achieve this? 如何实现呢? I tried to pass the DataTable as XML. 我试图将DataTable作为XML传递。 But due to the presence of NUll values it throws error not inserting. 但是由于NUll值的存在,它会引发未插入错误。

Error means, the XML tags not considering the NULL values, so it takes as 错误意味着,XML标记未考虑NULL值,因此将其视为

  <NewDataSet>
      <JobDetails> 
          <JobOrderID>1</JobOrderID>
          <CustomerID>2</CustomerID>
      </JobDetails>
      <JobDetails> 
          <JobOrderID>2</JobOrderID>
          <CompanyID>1</CompanyID>
      </JobDetails>
      <JobDetails> 
          <JobOrderID>3</JobOrderID>
          <CustomerID>5</CustomerID>
          <CompanyID>4</CompanyID>
      </JobDetails>
  </NewDataSet>

It neglects the tags which contains NULL. 它忽略了包含NULL的标签。 So the insertion not takes place. 因此插入不会发生。

Normally you would insert from a DataTable via an adapter (such as System.Data.SqlClient.SqlDataAdapter ). 通常,您将通过适配器 (例如System.Data.SqlClient.SqlDataAdapter )从DataTable插入。 This will handle the nulls etc. You could also use SqlBulkCopy if it is very large (although you need to be careful about transaction logs etc in that case, as a bulk insert might not be fully logged, IIRC). 这将处理null等。如果它很大,也可以使用SqlBulkCopy (尽管在这种情况下,您需要注意事务日志等,因为IIRC可能不会完全记录大容量插入)。

caveat: personally, I would be using a DataTable here anyway - I'd be using regular typed classes. 警告:就我个人而言, 无论如何我都会在这里使用DataTable我将使用常规类型的类。

In a case to get a DataTable into another table, use the SqlBulkCopy class. 如果要将DataTable放入另一个表,请使用SqlBulkCopy类。 Ensure your values that are null are set to DBNull.Value as opposed to 'null' and use the SqlBulkCopy to set the destination table name 确保将null值设置为DBNull.Value而不是'null'并使用SqlBulkCopy设置目标表名称


using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
   bulkCopy.DestinationTableName = destinationTableName;
   bulkCopy.WriteToServer(yourDataTable);
}

I think it would be easier if you follow the method described in the following article: 我认为,如果遵循以下文章中描述的方法,会更容易:

Use DataTable to update table in Database 使用DataTable更新数据库中的表

I think you can replace NULL with their default value for ex. 我认为您可以将NULL替换为其默认值。 in companyId field NULL replace with -1 or 0 and in SQL Server you can again replace -1 or o to NULL . 在companyId字段NULL将-1或0替换为-1,在SQL Server中,您可以再次将-1或o替换为NULL

Through replacing ID will be part of XML . 通过替换ID将成为XML一部分。 I know it is not good solution. 我知道这不是一个好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM