Asp.net connection access and mssqlserver database Method

Source: Internet
Author: User
Tags mssqlserver
To connect to Access, you need to import additional namespaces. Therefore, the first two using commands are required! The strConnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used. & quot; ProviderMicrosoft. jet. oleDb.4.0; & quot; is the index data provider. Here, the MicrosoftJet engine is used, that is, the Acc connection to Access requires an additional namespace to be imported. Therefore, the first two using commands are available, this is essential!

The strConnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used.

"Provider = Microsoft. jet. oleDb.4.0; "is the index data provider. Here, the Microsoft Jet Engine is used, that is, the Data Engine in Access ,. net is connected to the Access database.

"Data Source = C: BegASPNETNorthwind. mdb" indicates the location of the Data Source. Its standard format is "Data Source = MyDrive: MyPathMyFile. MDB ".

Instance

Using System;
Using System. Data;
Using System. Data. OleDb;
Using System. Configuration;
Using System. Windows. Forms;

Namespace WindowsFormsApplication1
{

///


/// Condb Summary
///
Public class DBHelper
{
Private OleDbConnection conn;
Private OleDbDataAdapter oda = new OleDbDataAdapter ();
Private OleDbCommand cmd;
Private DataSet myds = new DataSet ();
Public DBHelper ()
{
//
// TODO: add the constructor logic here
//
Conn = new OleDbConnection ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Application. StartupPath + @ "/db/dbtest. mdb ");
}
Public DataSet getDS (string strSQL)
{
Myds = new DataSet ();
Oda = new OleDbDataAdapter (strSQL, conn );
Oda. Fill (myds );
Return myds;
}

Public DataSet getDS2 (string strSQL, int si, int mi)
{
Conn. Open ();
Myds = new DataSet ();
Oda = new OleDbDataAdapter (strSQL, conn );
Oda. Fill (myds, si, mi, "tab1 ");
Conn. Close ();
Return myds;

}

Public bool setDS (string strSQL)
{
Conn. Open ();
Cmd = new OleDbCommand (strSQL, conn );
Cmd. ExecuteNonQuery ();
Conn. Close ();
Return true;
}
}

}

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

 

:
1. the "@" symbol after "+ =" prevents the "" in the subsequent string from being parsed as an escape character.
2. If the database file to be connected is in the same directory as the current file, you can use the following method to connect:
StrConnection + = "Data Source = ";
StrConnection + = MapPath ("Northwind. mdb ");
In this way, you can write a lot of things!
3. Note that parameters in the connection string must be separated by semicolons.

"OleDbConnection objConnection = new OleDbConnection (strConnection);" this sentence uses the defined connection string to create a link object. In the future, we will deal with this object for database operations.

"ObjConnection. Open ();" this is used to Open the connection. Now, the connection to the Access database is complete. Other operations (insert, delete...) can be found in related books.

Connect to SQL Server

Sample Code:
Program code:

--------------------------------------------------------------------------------
2000:

SqlConnection conn = new SqlConnection ("Data Source = (local); Initial Catalog = ArticleSystem; User ID = username; Password = pwd; Pooling = true ")

2005

SqlConnection conn = new SqlConnection ("Data Source = XLXCNBOOKSQLEXPRESS; Initial Catalog = ArticleSystem; User ID = username; Password = pwd; Pooling = true ")

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


Application

Using System. Data. SQL;
Using System. Data. SqlClient;
Private string ConnectionString = "user id = sa; password = 1234234; server = 11111; database = m23 ";
Private SqlConnection con = null;

SqlConnection conn = new SqlConnection (ConnectionString );
SqlCommand SCD = new SqlCommand ("* from 333", conn );
SDA. SelectCommand = SCD;
SDA. Fill (DT );

Connectstring is the string used to connect to the database. You all know that 111 is the service period name. database is the database.
The following is how to download the 333 table content in the m23 database to the local table called dt.
Then modify the local table using DT. rows [0] ["field name"] = value;
That's all.
Explanation:

There is no major difference between the mechanism for connecting to the SQL Server database and the Access mechanism, but it only changes the different parameters in the Connection object and Connection string.

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

The second is his connection string. We will introduce them one by one (note: the parameters are separated by semicolons ):
"User id = sa": the authenticated user name used to connect to the database is sa. It also has an alias "uid", so we can also write this statement as "uid = sa ".
"Password =": The verification password for connecting to the database is blank. Its alias is "pwd", so we can write it as "pwd = ".
Note that your SQL Server must have a user name and password set to log on. Otherwise, you cannot log on using this method. if your SQL Server is set to Windows logon, you do not need to use the "user id" and "password" methods to log on here, you need to use "Trusted_Connection = SSPI" to log on.
"Initial catalog = Northwind": the data source used is "Northwind". Its alias is "Database". This statement can be written as "Database = Northwind ".
"Server = YourSQLServer": Use a Server named "YourSQLServer". Its alias is "Data Source", "Address", "Addr". If you are using a local database
If the Instance name is defined, it can be written as "Server = (local) Instance name". If the instance is a remote Server) "Replace with the name or IP address of the remote server.
"Connect Timeout = 30": the connection Timeout is 30 seconds.

Here, the constructor used to create a connection object is SqlConnection.

The rest is no different from Access!


C # Notes for connecting to Access:

1. the "@" symbol after "+ =" prevents the "" in the subsequent string from being parsed as an escape character.

2. If the database file to be connected is in the same directory as the current file, you can use the following method to connect:

StrConnection + = "Data Source = ";
StrConnection + = MapPath ("Northwind. mdb ");

In this way, you can write a lot of things!

3. Note that parameters in the connection string must be separated by semicolons.

OleDbConnection objConnection = new OleDbConnection (strConnection );

This sentence uses the defined connection string to create a link object. In the future, we will deal with this object for database operations.

ObjConnection. Open ();

This is used to open the connection. At this point, the connection to the Access database is complete.

C # The precautions for connecting to Access are introduced here. I hope you can understand and learn how to connect to Access C.

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.