asp.net connect access with MSSQL Server database methods

Source: Internet
Author: User
Tags constructor mssql mssql server access database name database

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

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 tutorials. NET is connected to the Access database.

"Data Source=c:begaspnetnorthwind.mdb" is the location that indicates the data source, and his standard form 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
{

<summary>
Summary description of 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 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;
}
}

}

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

 

PS Tutorial:
1. The "@" symbol at the end of "+ =" is to prevent the "" in the following 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:

--------------------------------------------------------------------------------
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 ("SELECT * FROM 333", conn);
Sda. SelectCommand = SCD;
Sda. Fill (DT);

ConnectString is the string that connects the database you know it. 111 is the service name database is a data base
Here is the implementation of the M23 database to download the contents of the 333 tables into the local table called DT
Then modify the local surface with dt.rows[0]["field name"]=value;
It's okay.
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=".
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 the instance name is defined, it can be written as "server= (local) instance name", or 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!


C # connections to connect to access need to be aware of things:

1. The "@" symbol at the end of "+ =" is to prevent the "" in the following 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. So far, the connection to the Access database is complete.

C # connections Connect to access considerations to introduce you here, and I hope it helps you understand and learn about C # connection connections to 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.