Pregunta

I wrote a method which copies a table from SQL Server to a .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. 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. Do I miss something or is there a better way to insert this kind of information into a DataTable?

¿Fue útil?

Solución

Have a look at using SqlDataAdapter.FillSchema Method with your code.

Adds a DataTable to a DataSet and configures the schema to match that in the data source.

As an example also have a look at DbDataAdapter.FillSchema Method (DataSet, SchemaType)

A FillSchema operation adds a DataTable to the destination DataSet. It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

• AllowDBNull

• AutoIncrement. You must set AutoIncrementStep and AutoIncrementSeed separately.

• MaxLength

• ReadOnly

• Unique

FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

• If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the 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.

• If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the 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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top