使用OLE DB数据提供程序访问MSSQL数据库
使用OleDbDataReader来读取数据
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; namespace OleDbProvider { class Program { static void Main(string[] args) { string connString = @"Provider=SQLOLEDB;Data Source=SNH;Password=123456;User ID=sa;Initial Catalog=Northwind"; string sql = @"select * from employees"; OleDbConnection conn = null; OleDbDataReader reader = null; try { conn = new OleDbConnection(connString); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); reader = cmd.ExecuteReader(); Console.WriteLine("This program demonstrates the use of " + "the OLE DB Data Provider."); Console.WriteLine("Querying database {0} with query {1}\n", conn.Database, cmd.CommandText); Console.WriteLine("First Name\tLast Name\n"); while (reader.Read()) { Console.WriteLine("{0} | {1}", reader["FirstName"].ToString().PadLeft(10), reader[1].ToString().PadLeft(10)); } } catch (Exception e) { Console.WriteLine("Error: " + e); } finally { reader.Close(); conn.Close(); } Console.ReadKey(); } } }