Asp.net connection to SQL Server

Source: Internet
Author: User
Tags oracleconnection

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 ();
Bytes --------------------------------------------------------------------------------------------------------------------

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 the local database is used and the Instance name is defined, you can enter "Server = (local) \ Instance name". If it 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.
Bytes ------------------------------------------------------------------------------------------------------------------------

Asp.net connection to Oracle Database
Bytes ----------------------------------------------------------------------------------------------------------------------

String connectionstring = "Data Source = sky; user = system; Password = manager;"; // write the connection string
Oracleconnection conn = new oracleconnection (connectionstring); // create a new connection
Try
{
Conn. open ();
Oraclecommand cmd = conn. createcommand ();
Cmd. commandtext = "select * From mytable"; // write an SQL statement here
Oracledatareader ODR = cmd. executereader (); // create an oracledatereader object
While (ODR. Read () // read data. If ODR. Read () returns false, it indicates the end of the record set.
{
Response. Write (ODR. getoraclestring (1). tostring (); // output field 1. This number is a field index. How to use the field name remains to be studied.

}
ODR. Close ();
}
Catch (exception ee)
{
Response. Write (EE. Message); // if an error occurs, the error message is output.
}
Finally
{
Conn. Close (); // close the connection
Certificate ---------------------------------------------------------------------------------------------------------------------------------------

Asp.net connection access
Certificate -----------------------------------------------------------------------------------------------------------------------------------------

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 ();

......

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

Explanation:

To connect to the Access database, 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.

"Provider = Microsoft. jet. oledb.4.0; "is the index data provider. Here we use the Microsoft Jet Engine, which is the Data Engine in access. Asp.net is connected to the access database.

"Data Source = c: \ begaspnet \ northwind. mdb" indicates the location of the data source. Its standard format is "Data Source = mydrive: mypath \ myfile. mdb ".

PS:
1. the "@" symbol after "=" prevents the "\" symbol 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.
Certificate ------------------------------------------------------------------------------------------------------------------------------------

Asp.net connection to my SQL database
Certificate -----------------------------------------------------------------------------------------------------------------------------------

Connection:

String connstr = "Server = localhost; user id = root; Password =; database = AA; pooling = false ";//

Connection string
Mysqlconnection conn = new mysqlconnection (connstr); // construct a database connection

try
{< br> Conn. open (); // open the connection
mysqlcommand cmd = new mysqlcommand ("select * From list", Conn); // construct the query command
This. datagrid1.datasource = cmd. executereader (); // executes the query, returns a datareader, and sets the data source of datagrid1 as the datareader
This. datagrid1.databind (); // datagrid1 Data Binding
Conn. close (); // close the connection
}< br> catch (mysqlexception ex) // catch an exception
{< br> response. write (ex. message); // An error occurred while writing data to the page
}

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.