Simple common .NET database connection

xiaoxiao2021-03-05  26

ADO.NET uses OLE DB.NET SQL Server.net ODBC.NET three data providers for different data source services, which is more realistic and efficient.

1 Connect Access <% @ Import namespace = "system.data"%> <% @ Import name "%>" system.data.oledb%> string strConnection = "provike = C: //7418Begaspnetdbcs//datastores//northwind.mdb; "; OLEDBConnection Objconnection = New OLEDBCONNECTION (STRCONNECTION);

String strsql = "SELECT SuppnlierID, CompanyName from Suppliers"; OLEDBCommand Objcommand = New OLEDBCOMMAND (STRSQL, Objconnection);

Objconnection.open (); dgsuppliers.datasource = Objcommand.executeReader (); dgsuppliers.database (); objconnection.close ();

2 Connect SQL using SQL Server.net

<% @ Import namespace = "system.data"%> <% @ Import namespace = "system.data.sqlclient"%>

String strconnection = "server = (local) // netsdk; database = northwind; integrated security = true;"; sqlConnection Objconnection = new sqlConnection;

String strsql = "select firstname, lastname, country" "from Employees"; sqlcommand objcommand = new SQLCOMMAND (STRSQL, Objconnection);

Objconnection.open (); Dgnamelist.dataSource = objcommand.executeReader (); dgnamelist.database (); objconnection.close (); Objconnection.Close ();

3 Connect to OLEDB.NET to connect 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 Connecting XML data sources, using 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 ();

转载请注明原文地址:https://www.9cbs.com/read-32564.html

New Post(0)