Compiling the sqlhelper class in c #(1 ),

Source: Internet
Author: User

Compiling the sqlhelper class in c #(1 ),

In the project development of the. net platform, it is necessary to understand the principles of the SqlHelper class when database interaction is used.

Step 1:

I will take the WPF project development as an example. create an App first. config (application configuration file ). note: In VS, the new file name is App1.config by default. you must change the name back. no matter what you write in App1.config, the project will not be loaded. the file name must be App. config. of course, if you are doing web development, you can create a new web. config configuration file, same principle.

Create this configuration file to write necessary information for database connection, such as the server address, database name, user name, and password.

    

1 <configuration>2     <connectionStrings>3         <add name="connStr"  connectionString="Data Source=127.0.0.1;Initial Catalog=db_mytest;User Id=user;Password=111"/>4     </connectionStrings>5 </configuration>

Step 2:

The configuration file has been written. Now we will write the SqlHelper class.

When writing a class, you must have a clear idea, know the specific steps, and step by step.

The first step is to establish a connection to the database, and then define the add, delete, modify, and query method. The Code is as follows.

    

 1 class SqlHelper 2 { 3     private string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 4     public int ExecuteNonQuery(string sql) 5     { 6         using(SqlConnection conn = new SqlConnection(connStr)) 7         { 8             conn.Open(); 9             using(SqlCommand cmd = conn.CreateCommand())10             {11                 cmd.CommandText=sql;12                 return cmd.ExecuteNonQuery();13             }14         }15     }16 }                                            

Of course, I just wrote an add, delete, and modify method. This method returns an int type data, indicating the number of affected rows in the table. the following is the query method. Because this method returns the object type, the return type of the function is object.

Public object ExecuteScalar (string SQL) {// Similarly, establish a connection using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); // Open the connection using (SqlCommand cmd = conn. createCommand () {cmd. commandText = SQL; SqlDataAdapter adapter = new SqlDataAdapter (cmd); DataSet dataset = new DataSet (); adapter. fill (dataset); return dataset ;}}}

Of course, the above is just a simple principle. there are still many issues to make up for in specific projects. for example, to prevent SQL injection, for example, more convenient tabledata function return. I will explain it in a later 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.