Some little-known ways to connect a database to C #

Source: Internet
Author: User

Developers who have used VS2008 and VS2010 must know that installing this IDE automatically installs a compact version of the SQL database service SQLExpress, a database system that is less important than the Enterprise Manager, which means that it cannot be used to build data tables and some visual operations. If you encounter the project to use the SQL database can not be attached to the database service, resulting in the project in connection with the database will be prompted to find the database file and read failure. So is this lite SQL database service meant to be useless?

In fact, as long as we are connected to the database when the use of good it can be ~ ~

Here I present two ways to connect a database in C #. A need to attach a database, one is not, just need to install this lite version of SQL can be ...

Method One:

Connection code:

String strconn;

strconn = "Data source= (local);";

strconn + = "Initial catalog=student;";

strconn + = "User id=sa;";

strconn + = "password=;";

SqlConnection con = new SqlConnection (strconn); Connect to the database by user name and password

SqlConnection con = new SqlConnection ("Data source= (local); Initial catalog=library;integrated security=true"); Connecting to the database through System user authentication

Con. Open ();

SqlDataAdapter thisadapter = new SqlDataAdapter ("select * from reader where barcode = '" + TxM + "'", con);

SqlCommandBuilder Thisbuilder = new SqlCommandBuilder (thisadapter);

DataSet Thisdataset = new DataSet ();

Thisadapter. Fill (Thisdataset, "reader");

...//The next is the operation of the data

This method must be attached to the database file before it can be accessed ...

The advantage is that the database can be visualized directly through Enterprise Manager, such as making full changes to the records ... In case of data errors, in the system is not good to modify the time can be forced to maintain. At the same time, if you update the system later, add new database requirements, such as adding a field can be inside the operation.

Of course, this is also a disadvantage, the change of data may lead to the destruction of the database of some rules and data consistency, severe words can lead to catastrophic data crashes.

Method Two:

Connection code:

SqlConnectionStringBuilder Connectstringbuilder = new SqlConnectionStringBuilder ();

Connectstringbuilder.   DataSource = @ "(local)/sqlexpress"; Specify the database service

Connectstringbuilder. AttachDbFileName = @ "| Datadirectory|/data/library.mdf "; database files, specified in relative addresses. Note format

Connectstringbuilder. IntegratedSecurity = true;

Connectstringbuilder. Userinstance = true;

SqlConnection thisconnection = new SqlConnection (Connectstringbuilder. ConnectionString);

SqlDataAdapter thisadapter = new SqlDataAdapter ("select * from reader where barcode = '" + TxM + "'", thisconnection);

DataSet dt = new DataSet ();

Thisadapter. Fill (DT, "info");

...//The next is the operation of the data

The biggest thing about this approach is that we don't have to install 600M (MSSQL2000) or 1G (MSSQL2005) database systems, just install the SQL Lite database service (22M or so). And you don't have to attach the database ...

But this advantage also brings fatal disadvantage, that is, the first time to connect the database is a bit slow, it does not matter, but it will reformat the database file structure, resulting in some data format changes, so after the connection such a database file can not be attached to the database system again (will error). In other words, if you want to modify the database, such as adding fields and tables, etc. is unlikely to be implemented.

But it also brings security to a certain extent.

The above two kinds of database connection methods are different, whether to use depends on the actual application requirements of your system ...

(Note: Both of these methods are debugged in Vs2008+mssql ... )

Some little-known ways to connect a database to 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.