C # Some little-known methods for connecting to the database,
Developers who have used VS2008 and VS2010 certainly know that a lite version of SQL database service SqlExpress will be automatically installed when the IDE is installed. This database system lacks the most important enterprise manager, that is to say, you cannot use it to create data tables and perform visualization operations. If the SQL database is used in a project, it cannot be appended to the database service. As a result, the project will prompt that the database file cannot be found when connecting to the database, and the read will fail. So does this Lite version of SQL database service mean nothing?
In fact, it may not be necessary, as long as we make good use of it when connecting to the database ~~
Here I will introduce two methods for connecting to the database in C. You only need to install the lite version of SQL...
Method 1:
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 using the user name and password
// SqlConnection con = new SqlConnection ("Data Source = (local); Initial Catalog = library; Integrated Security = True"); // connect 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 step is to operate the data.
In this method, you must attach a database file to access the file...
The advantage is that you can directly perform visualized operations on the database through the Enterprise Manager, such as modifying the record with full permissions... In case of data errors, force maintenance can be performed when the system is difficult to modify. At the same time, if the system is updated later, new database requirements will be added. For example, you can add fields to the system.
Of course, this is also a disadvantage. Modifying data may cause damage to some Database Rules and data consistency. Serious changes may lead to catastrophic data crashes.
Method 2:
Connection code:
SqlConnectionStringBuilder connectstringbuilder = new SqlConnectionStringBuilder ();
Connectstringbuilder. DataSource = @ "(local)/sqlexpress"; // specify the Database Service
Connectstringbuilder. AttachDBFilename = @ "| DataDirectory |/data/library. mdf"; // specifies the database file by using the relative address. 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 step is to operate the data.
The biggest difference in this method is that we do not need to install a 600 M (MSSQL2000) or 1G (MSSQL2005) database system. We only need to install the SQL lite Database Service (around 22 M) you can. And you don't need to attach a database...
However, this advantage also brings a fatal disadvantage, that is, it will be slow when you connect to the database for the first time, which does not matter, but it will format some structure of the database file, leading to changes in some data formats, therefore, such database files cannot be attached to the database system again (an error will be reported ). That is to say, it is impossible to modify the database later, such as adding fields and tables.
However, this also brings security to some extent.
The above two database connection methods have their own merits. whether to use them depends on the actual application requirements of your system...
(Note: These two methods have passed the debugging in VS2008 + MSSQL ...)
How to arrange the number of C in A combination?
A32 is an arrangement. C32 is a combination.
For example, A32 is 3 multiplied by 2 to 6.
A 6 3 is 6*5*4
That is, starting from A large number, multiply the number next to it to indicate the number of numbers. A 7 2 equals 7*6*2, and there are two numbers. A 5 2 = 5*4.
Then C 3 2 is divided by A number. For example, C 3 2 is A 3 2 and then divided by A 22.
C 5 3 is A 5 3 divided by A 3 3
Are you quite clear about it?
How to arrange the number of C in A combination?
A32 is an arrangement. C32 is a combination.
For example, A32 is 3 multiplied by 2 to 6.
A 6 3 is 6*5*4
That is, starting from A large number, multiply the number next to it to indicate the number of numbers. A 7 2 equals 7*6*2, and there are two numbers. A 5 2 = 5*4.
Then C 3 2 is divided by A number. For example, C 3 2 is A 3 2 and then divided by A 22.
C 5 3 is A 5 3 divided by A 3 3
Are you quite clear about it?