C # Three ways to connect to a database __ database

Source: Internet
Author: User
Tags odbc

The first method is to connect to the database directly through the database's username, password and so on.

such as: private void sqlconn ()

{

Sqlconncetion conn = new SqlConnection ("server=.;D Atabase=pubs; pwd=; Uid=sa; ");
sqlconncetion cmd = new SqlCommand ("select*from [table]", CMD);
DataSet ds = new DataSet ();
SqlDataAdapter ADP = new SqlDataAdapter (cmd);
Adp. Fill (DS);

}

Where SqlConnection is a database connection class, SqlDataAdapter is a data adapter, SqlCommand is a data manipulation command, that is, executing the SQL language.

Description, use SqlConnection to connect to the database, and then use SqlCommand to define the SQL query statement, and then define a dataset to store the results of the query, while SqlDataAdapter is the transformation bridge between the database and the dataset.

The second method: Get the configuration information of the database through configurationmanager.connectionstring. Make a connection to the database.

such as: public static int Excutenonquery (string sql,params sqlparameter[] parameters)

{

String connstr = configurationmanager.connectionstrings["ConnStr"]. conncetionstring;

Using (Sqlconncetion conn=new SqlConnection (ConnStr))

{

Conn.Open ();

using (SqlCommand cmd = conn. CreateCommand ())

{

Cmd.commandtext = SQL;

foreach (SqlParameter parameter in parameters)

{

Cmd. Parameters.Add (parameter);

}

return CMD. ExecuteNonQuery ();

}

}

}

Description

(1) connstr=configurationmanager.connectionstring["ConnStr" through a string. ConnectionString to obtain some configuration information about the database connection, namely "data source", "Database name", "username", "password", and keep this information in ConnStr.

(2) SqlConnection conn = new SqlConnection (CONNSTR); Create a database Connection object conn and connect to the database with Conn. Open ();

(3) SqlCommand cmd = new SqlCommand ("SELECT * form [table]", conn);

or String sql = "SELECT * from [table]";

SqlCommand cmd = conn. CreateCommand ();

Cmd.commandtext = SQL; This saves the SQL statement that accesses the database to CMD.

(4) Establish SqlDataAdapter objects and dataset objects

DataSet myDataSet = new DataSet (), creating a DataSet object,

SqlDataAdapter mydataadapter = new SqlDataAdapter (cmd), creating a data adapter object.

(5) Filling DataSet:myDataAdapter.Fill (myDataSet); in fact, the SqlDataAdapter data adapter is a bridge between a database and a dataset (DataSet). The results of the SQL statement on the database operation are uploaded to the SqlDataAdapter object, and the result is populated into the dataset through the Sqldataadapter.fill (DataSet) method.

The third method: using the Factory method

DbProviderFactory m_dbproviderfactory = dbproviderfactories.getfacory (configurationmanager.connectionstring[" Constr "]. ProviderName);/Get Factory

DbConnection conn = M_dbproviderfactory.createconncetion ();//Create Connection

DbCommand cmd = M_dbproviderfactory.createcommand ();//create command

Cmd. Connection = conn;

Cmd.commandtext = strSQL;

Cmd.commandtype = CommandType.Text;

DbDataAdapter dapter = M_dbproviderfactory.createdataadapter ();//Create Adapter

Dapter. SelectCommand = cmd;

Dapter. Fill (DataSet ds = new DataSet ());//Fill

return DS;

Description

The above statement uses configurationmanager.connectionstring["Constr"]. ProviderName gets the name of the current database, so it can be applied to any database, and if so, the different databases use different languages: sqlserver:dbproviderfactory factory = Dbproviderfactories.getfactory ("System.Data.SqlClient"); Oracle:dbproviderfactory factory = dbproviderfactories.getfactory ("System.Data.OracleClient"); Odbc:dbproviderfactory factory = dbproviderfactories.getfactory ("System.Data.Odbc"); Access:dbproviderfactory factory = dbproviderfactories.getfactory ("System.Data.OleDb");

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.