C # SQL Server Operation

Disclaimer: The road has a lot to learn referral of place, hope to help you on it! https://blog.csdn.net/mingzaiwang/article/details/53504529

Operation can generally be divided into three steps:

First, a database connection object is created using sqlconnection;

The second, then sqlcommand object is responsible for calling for the implementation of sql statements and stored procedures;

Third, for "results" after the sql statements and stored procedure calls for operation.

I wrote a bit rough because the Internet is through a combination of their own writing system to an MVC student rough reply.

The results of treatment are generally divided into two categories:

A, sqldatareader direct line by line reading data set;

B, dataset joint sqldataadapter to operate the database.

When the need is certainly a demand to develop according to their own needs analysis before we are ready to use them, so the first thing we have to understand their different places.

sqldatareader time with the remote database connected to the server, the remote data forms "flow" of the one-way transmission to the client, it is "read-only". Because it is direct access to the database, so the efficiency is very high, but not convenient to use.

Having SqlDataReader, cut down and we said at SqldataAdapter dataset (Table Collection Object)

Dataset disposable acquired from the data source to the local data, and create a mini-database (comprising tables, rows, columns, the relationship between the rules, tables, etc.) locally, may be disconnected during the connection to the server, using the object operation sqlDataAdapter " local micro-database ", after the one-time update to a remote database server by sqldataAdapter. This way is more convenient and simple to use. However, if the relatively poor performance of the above, then the amount of data. Generally do not reach one million level is negligible.

 

let's start:

Define the connection string

 

string connectString = "Data Source=.;Initial Catalog=Student;Integrated Security=True";

Explanation: Data source Select the server in general we all understand the localhost; Initial Catalog represents the database name of the connection; Intergrated Security = True representation integrated authentication;.

 

As well as that usually go and get a link to a remote server to a specific account management:

public static string cnstr = "server = .; database = database name; User Id = designated account; pwd = specify a password;";

Next we enter a subject SqlConnection object

First need to reference system.data.sqlclient

Then add the namespace: system.data.sqlclient.sqlconnection

Examples of database connection object, and then open the connection

 

SqlConnection sqlCnt = new SqlConnection(connectString);
sqlCnt.Open();

After the operation is completed must not forget to close the connection:

 

 

sqlCnt.Close();

Began the most fun part of the SqlCommand object inside it involves more, there are more fun:

First you need to reference the same sqlconnection same: system.data.sqlclient

 

Namespace: System.Data.SqlClient.SqlCommand;

 

 

 

 

 

Guess you like

Origin blog.csdn.net/mingzaiwang/article/details/53504529