Data | Database in the front, I have said how to operate an Access database in a BETA2 environment, this time we'll look at how to get through ADO. NET to operate the SQL Server database!
First we need to know that in ADO. NET environment, we operate by System.Data.SqlClient this namespace, and a System.Data.SqlTypes namespace describes the type of SQL Server's field, but it does not directly participate in the operation of the database. So we focus on the use of System.Data.SqlClient!
By convention, I'll list my routines first: This program is what I use when I write a user registration system, I look at the program first:
Using System;
Using System.Data;
Using System.Data.SqlClient;
Obtain user information through the user ID number
Public UserInfo GetUserInfo (string id)
{
SqlConnection Myconn=sohotool.setconn (); Look at the notes a myconn. Open ();
Set up SQL query statements
String selectstr= "select * from UserInfo where id=" + ID;
try{
SqlCommand mycmd = new SqlCommand (selectstr,myconn);
SqlDataReader mysqlreader = myCMD. ExecuteReader ();
if (Mysqlreader. Read ())//If the user exists, enter the required attributes!
{
this.i_id= (int) mysqlreader["id"];
this.s_name=mysqlreader["Name"]. ToString ();
this.s_nicheng=mysqlreader["Nicheng"]. ToString ();
this.s_email=mysqlreader["Email"]. ToString ();
this.s_password=mysqlreader["Password"]. ToString ();
this.s_http=mysqlreader["http"]. ToString ();
this.s_oicq=mysqlreader["Oicq"]. ToString ();
this.s_tag=mysqlreader["Tag"]. ToString ();
this.i_charm= (int) mysqlreader["charm"];
this.i_score= (int) mysqlreader["score"];
this.i_bbswords= (int) mysqlreader["Bbswords"];
this.s_pic=mysqlreader["Pic"]. ToString ();
This.d_regtime= (DateTime) mysqlreader["Regtime"];
}
Else
{
Throw (New Exception ("No this user!") " ));
}
}
catch (Exception e)
{
Throw (The new Exception ("Database operation has an error!) "+ e.message));
}
MyConn. Close ();
return (this);
}
The above program implementation is: through the user ID number to obtain the user's personal data, below we see what new things in the program!
First we introduced the namespace System.Data.SqlClient in the beginning of the program, so we can use it to manipulate the SQL database!
1. At the beginning of the function we pass SqlConnection myconn=sohotool.setconn (); Get a connection to the SQL database.
2. After setting up the SQL query statement, we define a SqlCommand object and instantiate it in fact in ADO. NET most of the SQL statements can be easily executed with command, but if combined with dateset, it is necessary to introduce some other things, this to the back we say!
3. Defines the SqlDataReader object, executes the sq statement by SqlCommand the object, and then stores the result in the SqlDataReader object with the following statement: SqlDataReader Mysqlreader = myCMD. ExecuteReader ();
4. Set the properties and return the results you want
Read the previous article of the person, should be able to feel, in fact, the operation of SQL database and operation of Access database is no different, nothing more than the name space, COMMAND, reader's writing changed a sample AH! Oh, the actual situation is almost like this!
Note One, the above program I get the SQL database connection is used by my own class, below me this program also write out!
Namespace Soholife
{
Using System;
Using System.Data;
Using System.Data.SqlClient;
public class Sohotool
{
Establish a connection to the SQL database
public static SqlConnection Setconn ()
{
String connstr= "server=soho;database=soholife;uid=sa;pwd=;";
SqlConnection tempconn= New SqlConnection (CONNSTR);
return (Tempconn);
}
}
}
This program I think do not need to explain the bar, use is to get the database connection, some personal experience is, BETA1, we can inherit connection, to build their own connection class, and in the BETA2, but can not do so, Do not know why to make the connection class into a sealed form!
Well, this time it's written here, next time, I'll talk about how to execute the update, INSERT, DELETE statement through the SqlCommand object! If friends have a problem, you can write to me to discuss!
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