How do I connect to a database and loop over a recordset in C#?

前端 未结 8 2198
孤独总比滥情好
孤独总比滥情好 2021-01-03 17:44

What\'s the simplest way to connect and query a database for a set of records in C#?

8条回答
  •  梦毁少年i
    2021-01-03 17:54

    If you are querying a SQL Server database (Version 7 and up) you should replace the OleDb classes with corresponding classes in the System.Data.SqlClient namespace (SqlConnection, SqlCommand and SqlDataReader) as those classes have been optimized to work with SQL Server.

    Another thing to note is that you should 'never' select all as this might lead to unexpected results later on if you add or remove columns to this table.

提交回复
热议问题