This week's work is coming to an end. The test is completed, and the release of the application is submitted and released. Today's work has stopped, so there is no new demand. study hard and go up every day, first of all, I would like to express my feelings at this time. I am very happy to take a bus with my wife to go to work today, because she wants to change to work and take 936 to Pudong. I can also take that car to work, it's just the first time. I'm very happy and excited. I hope my wife can fix the company in Pudong and move it to Pudong together, I work on the subway every day. A lot of Roman, start learning:
ADO. NET is essential when we are working on projects, and the content in it is extremely huge. Let's get started with a simple chat, which is based on ASP. NET,
First import the namespace: using system. Data. sqlclient;, followed by the sqlconnection class, sqlcommand also came, there are a lot more ....
When using the database, you must first open the connection,
Using (sqlconnection conn = new sqlconnection ("Server =.; uid = sa; Pwd = sa; database = pubs "))
{
String STR = "insert/update/delete..."; // This STR is the SQL statement to be executed
Sqlcommand cmd = new sqlcommand (STR, Conn );
Conn. open ();
Cmd. executenonquery ();
Conn. Close ();
}
Of course, there are a lot of such writing methods, but in combination with some performance considerations, I can accept this in some small and relatively simple projects. After the connection is opened, close the connection in time, when you need to operate the database, open the connection and try to avoid resource waste and pressure on the server. Of course, this is still a security defect. Let's talk about it later, here we will continue to talk about the main methods of sqlcommand class objects:
Executenonquery (): a command that executes a method without returning results. It is usually used to perform insert, delete, and update operations.
Execute the executescalar method to execute the command that returns a single value, as we usually verify the login, and check whether a piece of information exists like this:
Sqlcommand cmd = new sqlcommand ("select count (*) from users where uid = 'teracy 'and Pwd = 'teracy'", Conn );
The executereader () method is used to obtain data, which is relatively small. See the following section.CodeLet's explain the problem,
Private Void Sqlcommandexec ( String Querystr,
String Conn)
{
Using (Sqlconnection con = New Sqlconnection (
Conn ))
{
Sqlcommand command = New Sqlcommand (querystr, con );
Con. open ();
Sqldatareader Dr =
Command. executereader (commandbehavior. closeconnection );
Stringbuilder sb = New Stringbuilder ();
While (Dr. Read ())
{
SB. append (Dr [ " Pub_name " ] + " " + Dr [ " City " ] + " <Br> " );
}
Response. Write (sb. tostring ());
Con. Close ();
}
}
Of course, there are still many things in ADO. net. There is not so much time here. Another common difference between datareader and dateset is:
The former reads data only in read-only mode, and the latter is powerful, but it also has to pay the cost of resource consumption, another problem is the security considerations for writing SQL statements mentioned above. I will take some code for reference. Now I have a new requirement and I am about to start working.
1 Public Static Bool Updateuserpwd ( Int Customersysno, UID)
2 {
3 String Updatesql = @" Update users set
4 Pwd = @ PWD where
5 Id = @ uid ";
6 Sqlparameter [] parms = new sqlparameter []
7 {
8 New sqlparameter ( " @ Pwd " , Sqldbtype. varchar ),
9
10 New Sqlparameter ( " @ Uid " , Sqldbtype. INT ),
11
12
13 } ;
14 Parms [ 0 ]. Value = PWD;
15 Parms [ 1 ]. Value = UID;
16
17 Return Dataaccessfacade. execsqlcommand (updatesql, parms) > 0 ;
18 }
Let's take a good look at it. These are my understandings and may be some shortcomings for your reference, begin ............................. work now
Sorry, my wife was so hard to speak with her expression ability. I 'd like to express it in code. sqlcommand is mainly used in these methods. I remember that when I first came out, I was often asked such a question, no one seems to have asked these questions now,