Is there any other way to populate a datatable in ADO.Net without using the dataadaptor.Fill method?

Is there any other quick way to populate a data table in ADO.Net without using the Data adapter.Fill method?

+3
source share
2 answers

Yes, you can. Here is a quick example:

var results = new DataTable();
using(var connection = new SqlConnection(...))
using(var command = connection.CreateCommand())
{
   command.Text = "sql statement";
   var parameter = command.CreateParameter();
   parameter.Name = "name";
   parameter.Value = aValue;
   command.Parameters.Add(parameter);

   connection.Open();
   results.Load(command.ExecuteReader());
}
return results;

If you just need to create a data table, for example. To store data not coming from the database, you can create a new DataTable and fill it yourself, for example:

var x = new DataTable("myTable");
x.Columns.Add("Field1", typeof(string));
x.Columns.Add("Field2", typeof(string));
x.Columns.Add("Field3", typeof(int));

x.Rows.Add("fred", "hugo", 1);
x.Rows.Add("fred", "hugo", 2);
x.Rows.Add("fred", "hugo", 3);
+7
source

You can manually create DataTablestheir contents using methods of various types.

, ( .NET 1.1, , DataSet).

[ , .]

0

Source: https://habr.com/ru/post/1734755/


All Articles