Asp. NET how to connect to an Access or SQL Server database

Source: Internet
Author: User
Tags log connect access database

Connect to access

First look at an example code fragment:
Program code:

--------------------------------------------------------------------------------

Using System.Data;
Using System.Data.OleDb;

......

String strconnection= "provider=microsoft.jet.oledb.4.0;";
strconnection+=@ "Data Source=c:\begaspnet\northwind.mdb";

OleDbConnection objconnection=new OleDbConnection (strconnection);

......

Objconnection.open ();
Objconnection.close ();

......

--------------------------------------------------------------------------------

Explain:

Connecting to an Access database requires importing additional namespaces, so it is essential that you have the first two using command!

Strconnection This variable holds the connection string required to connect to the database, specifying the data provider to use and the data source to use.

"Provider=Microsoft.Jet.OLEDB.4.0;" Refers to the data provider, which uses the Microsoft Jet engine, which is the data engine in access, that ASP.net is connected to the Access database. Web Teaching Network

"Data Source=c:\begaspnet\northwind.mdb" is the location that indicates the data source, and his standard form is "Data Source=mydrive:mypath\myfile.mdb".

Ps:
1. The "@" symbol at the end of "+ =" is to prevent the "\" in the subsequent string from being resolved to an escape character.
2. If the database file you want to connect to is in the same directory as the current file, you can also connect by using the following methods:
strconnection+= "Data source=";
Strconnection+=mappath ("Northwind.mdb");
This will save you from writing a whole lot of stuff!
3. Be aware that the parameters in the connection string are separated by semicolons.

"OleDbConnection objconnection=new OleDbConnection (strconnection);" This is the use of a defined connection string to create a linked object, the future operation of the database we have to deal with this object.

"Objconnection.open ();" This is used to open the connection. At this point, the connection to the Access database is complete. The rest of the operation (INSERT, delete ...) Please refer to related books

Connecting to SQL Server

Example code fragment:
Program code:


--------------------------------------------------------------------------------
Web Teaching Network


Using System.Data;
Using System.Data.SqlClient;

...

String strconnection= "user id=sa;password=;";
strconnection+= "Initial catalog=northwind; Server=yoursqlserver; ";
strconnection+= "Connect timeout=30";

SqlConnection objconnection=new SqlConnection (Strconnection);

...

Objconnection.open ();
Objconnection.close ();

...

--------------------------------------------------------------------------------

Explain:

The mechanism for connecting to a SQL Server database is not much different from the mechanism for connecting to access, except that different parameters in the Connection object and the connection string are changed.

First, the namespace used to connect to SQL Server is not "System.Data.OleDb", but "System.Data.SqlClient".

And then there's his connection string, and we'll introduce a parameter (note: Separate the parameters by semicolons):
' User Id=sa ': The authentication user name for the connection database is SA. He also has an alias "UID", so we can also write "Uid=sa".
"Password=": The authentication password for the connection database is empty. His alias is "pwd", so we can write it as "pwd=".
Webjx.com


Notice here that your SQL Server must have been set up with a username and password to log in, otherwise you won't be able to log in this way. If your SQL Server is set up for Windows login, you don't need to use the user ID and password here. This way to log in, and you need to use "TRUSTED_CONNECTION=SSPI" to log in.
"Initial Catalog=northwind": The data source used is the "Northwind" database. His alias is "database", this sentence can be written as "Database=northwind".
"Server=yoursqlserver": Use a server named "YourSQLServer". His alias is "Data Source", "Address", "Addr". If you are using a local database and you have defined an instance name, you can write as " server= (local) \ instance name; If it is a remote server, replace "(local)" with the name or IP address of the remote server.
"Connect timeout=30": Connection timeout is 30 seconds.

Here, the constructor used to establish the connection object is: SqlConnection.

The rest is no different from Access!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.