几种常见的数据库连接方法 一、连接Access 数据库 1.使用已有 DSN 的连接字符串进行连接(ODBC) 使用 DSN 进行连接 //导入命名空间 using System.Data.Odbc; protected void Page_Load(Object sender,EventArgs e) { //设置连接字符串 String connstr=@"DSN=sample"; //实例化 Connection 对象 OdbcConnection myConnection = new OdbcConnection(connstr); //执行 Open 方法打开连接 myConnection.Open(); //执行 SQL 语句 OdbcCommand myCommand = new OdbcCommand("select * from sampletable",myConnection); //将查询的结果赋给 GridView 的数据源 gv.DataSource = myCommand.ExecuteReader(); //绑定 GridView gv.DataBind(); //关闭连接 myConnection.Close(); } 2.使用无 DSN 的连接字符串进行连接(ODBC) 不使用 DSN 进行连接 //导入命名空间 using System.Data.Odbc; protected void Page_Load(Object sender,EventArgs e) { //设置连接字符串 String connstr=@"Driver=Microsoft Access Driver (*.mdb);Dbq=c:\sample.mdb;"; //实例化 Connection 对象 OdbcConnection myConnection = new OdbcConnection(connstr); //执行 Open 方法打开连接 myConnection.Open(); //执行 SQL 语句 OdbcCommand myCommand = new OdbcCommand("select * from sampletable",myConnection); //将查询的结果赋给GridView 的数据源 gv.DataSource = myCommand.ExecuteReader(); //绑定GridView gv.DataBind(); //关闭连接 myConnection.Close(); } 3.使用连接字符串进行连接(OLEDB) OLEDB.NET Data Provider 支持的OLEDB Provider: SQLOLEDB:用来访问SQL Server 数据库 MSDAORA:用来访问Oracle 数据库 Microsoft.Jet.OLEDB.4.0:用来访问Access 数据库。 使用连接字符串 //导入命名空间 using System.Data.OleDb; protected void Page_Load(Object sender,EventArgs e) { //设置连接字符串 String connstr=@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\sample.mdb;"; //实例化OleDbConnection 对象 OleDbConnection myConnection = new OleDbConnection(connstr); //执行Open 方法打开连接 myConnection.Open(); //执行SQL 语句 OleDbCommand myCommand = new OleDbCommand("sel...