ado.net中用了ole db.net sql server.net odbc.net 三个数据提供者为不同的数据源服务,这样更有真对性和高效性。
1用oledb.net连接access
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb%>
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source=C://7418BegASPNETdbCS//datastores//Northwind.mdb;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSQL = "SELECT SupplierID, CompanyName FROM Suppliers";
OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
objConnection.Open();
dgSuppliers.DataSource = objCommand.ExecuteReader();
dgSuppliers.DataBind();
objConnection.Close();
2使用sql server.net连接sql
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
String strConnection = "server=(local)//NetSDK; database=Northwind; integrated security=true;";
SqlConnection objConnection = new SqlConnection(strConnection);
String strSQL = "SELECT FirstName, LastName, Country " + "FROM Employees";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
dgNameList.DataSource = objCommand.ExecuteReader();
dgNameList.DataBind();
objConnection.Close();
3用oledb.net连接excel
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source=C://7418BegASPNETdbCS//datastores//Inventory.xls; Extended Properties=Excel 8.0;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSQL = "SELECT * FROM Items WHERE Source='Dell'";
OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
4连接xml数据源,用了dataset中的东西
String strXMLFile =
"C://7418BegASPNETdbCS//datastores//Multiple_levels.xml";
lblXMLFileName.Text = strXMLFile;
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(strXMLFile);
dgServers.DataSource = objDataSet.Tables[0].DefaultView;
dgServers.DataBind();