Sqlhelper. CS

Source: Internet
Author: User
The sqlhelper. CS file encapsulates all database operations.

My sqlhelper. CS is a version that my classmates gave me. It may be different from others, but it has not changed much. Four files are involved in connecting to the database: sqlhelper. CS, Web. config, and Web applications to connect to the database. Program Sqltest. CS and sqltest. aspx (these two file names are obtained by themselves. They are associated with different suffix names with the same name ). Of course, there is also a database test on the (local) server (this is also self-built ). First, use visual studio.net 2003 to create a webapplication named sqltest. At this time, sqltest. CS, sqltest. aspx, and Web. config are automatically generated in the project. Add the sqlhelper. CS file to the project by adding it to the project through <add existing items> In the <project> menu. The sqlhelper. CS file can be downloaded from the Internet. You can search for it using Baidu or Google. In this way, the preparation is complete, and the configuration process is as follows. 1. to connect to the database, use the connection string in sqlhelper. CS. First in sqlhelper. in the sqlhelper class of CS, declare a connection string: public static string conn_string_lc = configurationsettings. appsettings ["conn_string_lc"]; conn_string_lc can be replaced with any name you understand. Note that the two conn_string_lc values in this sentence must be consistent. 2. Open the Web. config file and add an item to it. Used to record the actual value of the connection string. <Deleetask>
<Add key = "conn_string_lc" value = "Server =.; database = test; uid = sa; Pwd ="/>
</Appsettings> the conn_string_lc corresponds to the preceding sentence and must be consistent. Value is followed by the database configuration file, Server =. indicates a (local) server. Of course, you can change the vertex to the server name. Database = test indicates that the database source is test. uid = SA is a user name for the database test, sa is the default user of SQL Server 2000. You can also create different users. Pwd = is followed by the password of the corresponding user. 3. Add a dategrad control in sqltest. aspx. The default name is datagrid1. It will be used to display database data in the future. Add a label control. The default name is label1. It will be used to display the connection success or failure information in the future. 4. Add the program private void page_load (Object sender, system. eventargs E) to page_load in sqltest. CS)
{
Try
{// Create a dataset
Dataset mydata = new dataset (); // defines the query statement
String cmd = "select top 1 * from student"; // call sqlhelper. executedataset
Mydata = sqlhelper. executedataset (sqlhelper. conn_string_lc, commandtype. Text, CMD); // bind the database and DataGrid Control
Datagrid1.datasource = mydata. Tables [0];
Datagrid1.databind (); // display the successful connection information label1.text = "database connection successful ";
} // Display the catch (exception excp) error message for a database connection ){
Label1.text = excp. Message. tostring ();}
} Here we will focus on the sqlhelper. executedataset method. This method returns a DataSet object, which has nine methods with the same name that reload different signatures (that is, different parameters. In this example, we use one of the three parameters. The first parameter is the connection string of the configuration file, which corresponds to the previous one using sqlhelper. conn_string_lc; the second is the query command type. Use this commandtype. text is enough. Finally, it is a self-created query statement string that directly affects the content displayed in the datagrid1. In this example, all the fields in the first row of the student table in the test database are queried, the standard SQL language is used. 5. Note that if the namespace of sqlhelper. CS is the same as that of testsql. CS, the connection is successful. If they are different, add using da to testsql. CS, where da represents the namespace of sqlhelper. CS.

Related Article

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.