Need to get empty datatable in .net with database table schema

前端 未结 10 831
闹比i
闹比i 2021-02-07 04:40

What is the best way to create an Empty DataTable object with the schema of a sql server table?

10条回答
  •  醉话见心
    2021-02-07 05:21

    Here's what I did, which provides a blank DataTable ready to be used:

    SqlConnection _sqlConnection = new SqlConnection ();
    _sqlConnection.ConnectionString = @"Data Source=; Initial Catalog=; Integrated Security=False; User ID=;Password=";
    _sqlConnection.Open ();
    SqlCommand _sqlCommand = new SqlCommand ( "select * from DatabaseName.dbo.viewName", _sqlConnection ); 
    _dataSet = new DataSet ();
    _sqlDataAdapter = new SqlDataAdapter ( _sqlCommand );
    _sqlDataAdapter.Fill ( _dataSet );
    _schemaTable = new DataTable ();
    _sqlDataAdapter.FillSchema ( _schemaTable, SchemaType.Source );
    dataGridView.DataSource = _schemaTable;
    _sqlConnection.Close ();
    

提交回复
热议问题