Connect Access
First look at an example code segment: program code:
Using system.data; using system.data.oledb; ... string strconnection = "proviker = microsoft.jet.Oledb.4.0;"; strconnection = @ "data source = c: /begaspnet/northwind.mdb" OLEDBCONNECTION OBJCONNECTION = New OLEDBCONNECTION (STRCONNECTION); ... Objconnection.Open (); Objconnection.Close (); ......
Explanation: Connecting an Access database requires an additional namespace, so there is two two using commands, which is essential! StrConnection is stored in this variable is the connection string required to connect to the database. He specified The data source used and the data source to use. "Provider = microsoft.jet.Oledb.4.0;" refers to the data provider, where the Microsoft Jet engine is used, that is, the data engine in Access, ASP.NET is Connected by this and Access database. "Data Source = C: /BEGASPNET/NorthWind.mdb" is the location of the data source, his standard form is "Data Source = MyDrive: mypath / myfile.mdb" .ps: 1 "@" Symbol behind " =" is to prevent "/" to resolve "/" in the back string ". 2. If the database file to be connected, the current file is used in the same directory, you can use the following Method connection: strconnection = "data source ="; strconnection = mappath ("northwind.mdb"); this can save you a lot of things! 3. Pay attention to the semicolon between the parameters in the connection string To separate. "OLEDBCONNECONNECONNECTION = New OLEDBCONNECTION (STRCONNECTION);" This sentence is to build a link object with a defined connection string. We must deal with this object in the future. "ObjConnection.Open () "This is used to open the connection. So far, the connection to the Access database is complete. The rest (insert, delete ...) See the relevant books
Connect SQL Server
Example Code Dischart: Program Code:
Using system.data;
Using system.data.sqlclient;
...
String strconnection = "User ID = sa; password =;";
StrConnection = "Initial catalog = northwind; server = yoursqlser;";
StrConnection = "Connect Timeout = 30";
SqlConnection ObjConnection = New SqlConnection (STRCONNECTION);
...
ObjConnection.open ();
ObjConnection.Close ();
...
Explanation:
Connecting the SQL Server database The mechanism of connecting Access is not too big, just changing the different parameters in the Connection object and the connection string.
First, the namespace connected to SQL Server is not "system.data.oledb", but "system.data.sqlclient". Secondly, his connection string, let's introduce a parameter (Note: Division between parameters Separation):
"User ID = SA": Connect the verification user name of the database name to sa. He also has an alias "UID", so we can also write "UID = sa".
"Password =": The verification password that connects the database is empty. His alias is "PWD", so we can write "PWD =".
Note that your SQL Server must have set up a username and password to log in, otherwise you cannot log in with this way. If your SQL Server is set to Windows login, then you don't need to use "User ID" and " Password "to log in, but you need to use" Trusted_Connection = SSPI "to log in.
"Initial Catalog = Northwind": The data source used is "Northwind" database. His alias is "Database", this sentence can be written as "Database = Northwind".
"Server = yoursqlserver": Using a server named "Yoursqlserver". His aliasing "Data Source", "Address", "Addr". If you use a local database and define instance name, you can write as "Server = (local) / instance name "; if it is a remote server," (local) "replaces the name or IP address of the remote server.
"Connect Timeout = 30": The connection timeout is 30 seconds.
Here, the constructor used to establish the connection object is: SqlConnection.
The rest is nothing difference with Access!