简单常用.net数据库连接

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值