简体   繁体   English

如何将SQL Server表正确映射到DataTables?

[英]How to map SQL Server tables to DataTables correctly?

I wrote a method which copies a table from SQL Server to a .NET-DataTable: 我编写了一种将表从SQL Server复制到.NET-DataTable的方法:

public static DataTable SQLtoDataTable(string _connectionString, string _tableName)
{
    DataTable dt = new DataTable();
    string queryString = "SELECT * FROM " + _tableName;

    using (SqlConnection connection = new SqlConnection(_connectionString))
    {
        using (SqlCommand queryCMD = new SqlCommand(queryString))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(queryCMD))
            {
                queryCMD.Connection = connection;
                connection.Open();
                da.Fill(dt);
                connection.Close();
            }
        }
   }

   return dt;
}

Unfortunately this method does not seem to work 100% correctly. 不幸的是,这种方法似乎无法100%正确地工作。 While columns and row data are transferred 1:1, it seems like the AllowDBNULL value is set to true for every single column no matter if it is set to true or false in the database. 当列和行数据按1:1传输时,似乎AllowDBNULL中的每个列的AllowDBNULL值都设置为true ,无论该值设置为true还是false Do I miss something or is there a better way to insert this kind of information into a DataTable ? 我会错过什么吗?还是有更好的方法将这种信息插入到DataTable

Have a look at using SqlDataAdapter.FillSchema Method with your code. 看看将SqlDataAdapter.FillSchema方法与代码一起使用。

Adds a DataTable to a DataSet and configures the schema to match that in the data source. 将DataTable添加到DataSet并配置架构以匹配数据源中的架构。

As an example also have a look at DbDataAdapter.FillSchema Method (DataSet, SchemaType) 作为示例,还可以查看DbDataAdapter.FillSchema方法(DataSet,SchemaType)

A FillSchema operation adds a DataTable to the destination DataSet. FillSchema操作将DataTable添加到目标DataSet。 It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source: 然后,它将列添加到DataTable的DataColumnCollection中,并配置以下DataColumn属性(如果它们存在于数据源中):

• AllowDBNull •AllowDBNull

• AutoIncrement. • 自动递增。 You must set AutoIncrementStep and AutoIncrementSeed separately. 您必须分别设置AutoIncrementStep和AutoIncrementSeed。

• MaxLength • 最长长度

• ReadOnly • 只读

• Unique • 独特

FillSchema also configures the PrimaryKey and Constraints properties according to the following rules: FillSchema还根据以下规则配置PrimaryKey和Constraints属性:

• If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable. •如果SelectCommand返回一个或多个主键列,则它们将用作DataTable的主键列。

• If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. •如果没有返回主键列,但返回唯一列,则仅当所有唯一列均不可为空时,才将唯一列用作主键。 If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set. 如果任何列可为空,则将UniqueConstraint添加到ConstraintCollection,但是未设置PrimaryKey属性。

• If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable. •如果同时返回了主键列和唯一列,则主键列将用作DataTable的主键列。

The example used 使用的例子

DataSet dataSet = new DataSet(dataSetName);

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(
        "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection);

    DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers");
    mapping.ColumnMappings.Add("CompanyName", "Name");
    mapping.ColumnMappings.Add("ContactName", "Contact");

    connection.Open();

    adapter.FillSchema(dataSet, SchemaType.Mapped);
    adapter.Fill(dataSet);

    return dataSet;
}

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

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