Made a method to execute any sql query from the database, 1) prompt, but the DataTable itself accepts the result of the query from the database (that is, the DataTable does not drag all the data into itself and then does the query on the client side, or the query is base and only the result is given).

class Program { static void Main(string[] args) { string connectionString = @"Data Source=VLADIM\SQLEXPRESS;Initial Catalog=northwind;Integrated Security=True"; string qwery = "SELECT TOP 1000 [ProductID]\r\n ,[ProductName]\r\n FROM [northwind].[dbo].[Current Product List]"; GetZapros2(connectionString,qwery); Console.ReadKey(); } static void GetZapros2(string _connectionString, string qwery) { string connectionString = _connectionString; using (SqlConnection connection = new SqlConnection(connectionString)) if (connection.State != ConnectionState.Open) { connection.Open(); } var dt = new DataTable(); //получаем результат запроса в DataTable... using (var adapter = new System.Data.SqlClient.SqlDataAdapter(qwery, connectionString)) { adapter.Fill(dt); } //далее работаем с DataTable... foreach (DataRow dataRow in dt.Rows) { foreach (var item in dataRow.ItemArray) { Console.WriteLine(item.ToString() + "; "); } Console.WriteLine(); } } } 

2) How to effectively catch possible errors. The request for example gave an error, what will I see in the DataTable?

  • And try it yourself? - Andrey NOP

1 answer 1

  1. ADO.NET is a standalone data technology. When the DataAdapter finishes filling the DataTable , it closes the connection and you are working with a copy (clipping), which has no effect on the original data. When you want to pull the data back into the database, you send it your DataTable and it sends the data to the server. And it doesn’t fire all the data, but only the changed ones, in order to reduce traffic.
  2. You catch a mistake in the moment of filling and the DataTable will remain empty.
  • Thanks for the answer, I ask from those considerations what is more convenient when all the samples are made in the database, and not in a copy from the client, and the result of the sample flies back to the client. - Vladimr Vladimirovoch