C # Database programming

Source: Internet
Author: User
Tags ole

Tag: Source It's adapter project local data DAP next open

The use of the database is a very important technology, it is almost related to all programming languages. C # provides access to the ADO database in SQL Server and OLE db two. In comparison, the former is a connection to a SQL Server database, so accessing the SQL Server database can be very efficient, and the latter is relatively inefficient for a wide range of data operations. If you do not involve a special case of the database, their approximate approach is the same.

First of all, to connect to the database, you must have a Connection object connection, you can have SqlConnection and OleDbConnection, which connection object to use, depending on the type of database you need to connect to. Here are two ways to connect the code.

SQL Server data Access

String strconn = "Integrated security=sspi;initial catalog=mydatabase;data Source=mydatasource";

Integrated Security can be set to: true, false, yes, no, these four meanings are clear, can also be set to: SSPI, equivalent to true, it is recommended to use this instead of true. Integrated Security=sspi This indicates that the current Windows system user is logged on to the SQL Server server and an error occurs if the SQL Server server does not support this way of logging on. The Initial catalog is followed by the database data source followed by the resources.

SqlConnection conn = new SqlConnection (strconn);//Create Connection object

Conn. Open ();//Opens the connection, in the actual combat project, will wrap it up, plus Conn. The judgment of state = = Connectionstate.close

Ole DB data access

String strconn = "Provider=sqloledb;data source=localhost;initial catalog=mydatabase;integrated Security=SSPI";

OleDbConnection conn=new OleDbConnection (strconn);

Conn. Open ();

If the above code does not throw an exception, it means that the database has been successfully connected. The next step is to create DataAdapter to complete the work of accessing the database. DataAdapter's work is the basis of the dataset, which is to create a dataset and the middle tier of the database to coordinate access. According to the same type of database can be divided into SqlDataAdapter and OleDbDataAdapter two kinds of adapters.

SqlDataAdapter adapter = new SqlDataAdapter ();

String strSQL = "SELECT * from TableName";

Adapter. SelectCommand = new SqlCommand (strsql,conn);

SqlCommandBuilder MYCB = new SqlCommandBuilder (adapter);

DataSet myDataSet = new DataSet ();

Adapter. Fill (myDataSet, "table name");

This code uses the SqlDataAdapter, DataSet, SqlCommand, SqlCommandBuilder four objects. SqlDataAdapter's role is to be responsible for communication with the database access, while connected to the dataset, it has four very important command objects (also divided into SqlCommand and OleDbCommand), are access to the database must be used, The SelectCommand, InsertCommand, UpdateCommand, DeleteCommand objects are respectively. These command objects are specifically used to complete the query, insert, UPDATE, delete operations on the database, they are like four ministers, under the control of DataAdapter, respectively, in charge of their respective things. Among them, SelectCommand is the eldest of their four, which can be automatically constructed to generate another three. The process of constructing builds is to apply CommandBuilder. Before that, we just need to specify the SelectCommand object in DataAdapter.

C # Database programming

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.