Insert statement for connection between applications and databases

Source: Internet
Author: User

What we need to do is to connect the database to a Windows form application. Input a record on the interface to insert it into a table in the database.

When connecting applications and databases, make the following preparations:

① Create a database;

② Add several tables to the database;

③ Insert statement in SQL language learning: InsertInto [Table name] (Field List) Values (Value List );

④ Connect to the database through a program, create commands, and execute commands.

The previous two steps are simple and we can easily complete them.

The InsertInto statement is used to insert new rows into the table.

Syntax: InsertInto table name Values (value 1, value 2 ,....)

We can also specify the column for data insertion: InsertInto table name (column 1, column 2,...) Values (value 1, value 2 ,....

First look at the design of the form:

650) this. width = 650; "title =" image 1.png" src = "http://www.bkjia.com/uploads/allimg/131228/193FGZ5-0.png" alt = "133218949.png"/>

In this form, we set the gender to male in advance. You can apply the RadioButton control in future operations. You can select the DateTimePicker control in the control at your own time ), let's take a look at how the application is written:

/// <Summary> /// Click Time of the Add button /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void btnSave_Click (object sender, eventArgs e) {// string name = txtName from the form. text; string sex; if (this. radMan. checked = true) {sex = "1";} else {sex = "0";} DateTime birthday = this. dtpBirthday. value; string salary = this.txt Salary. text; // connect to the database string connString = @ "server =. \ sqlexpress; databas E = CardInfosDB; uid = sa; pwd = 199298; "; SqlConnection connection = new SqlConnection (connString); connection. open (); // create a command object SqlCommand command = connection. createCommand (); // organization SQL statement string SQL = "Insert Into CardInfos (name, sex, birthday, salary) Values (@ name, @ sex, @ birthday, @ salary) "; // The SQL statement to be executed to the Command object command. commandText = SQL; // complete the SQL statement SqlParameter [] ps = new SqlParameter [] {new SqlParameter ("@ nam E ", name), new SqlParameter (" @ sex ", sex), new SqlParameter (" @ birthday ", birthday), new SqlParameter (" @ salary ", salary )}; command. parameters. addRange (ps); // execute the SQL statement: Send the SQL statement to the database for execution, add only, do not query int x = command. executeNonQuery (); if (x! = 0) {this. label5.Text = "added successfully ...... ";} Connection. close ();} /// <summary> /// Click the cancel button // </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void btnCancel_Click (object sender, eventArgs e) {this. close ();}

When connecting to the database, we use the SqlConnection class to create a connection so that the database can communicate with the program. When executing the inserted command, we use the SqlCommand class, this is a command class. You can use the ExecuteNonQuery () method in it to execute our insert statement and add records to the database.

Now we can connect the database to the application, and add the desired data information to the database by using this connection, which is simple and clear.

P.S. When selecting the data type, we should select as few strings as possible. For example, salary, number, and gender, we can select other types such as money, int, and bit;

SQL statements are case insensitive.

This article from the "Lanting drunk beauty" blog, please be sure to keep this source http://7607889.blog.51cto.com/7597889/1300071

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.