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

前端 未结 8 2197
孤独总比滥情好
孤独总比滥情好 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 18:16

    That's definitely a good way to do it. But you if you happen to be using a database that supports LINQ to SQL, it can be a lot more fun. It can look something like this:

    MyDB db = new MyDB("Data Source=...");
    var q = from db.MyTable
            select c;
    foreach (var c in q)
      Console.WriteLine(c.MyField.ToString());
    

提交回复
热议问题