Display tables in the database to the form

Source: Internet
Author: User

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/193333I17-0.gif "alt =" y_0034.gif "/> after WinForm is connected to the database, the data in the form can be added to the database, the data in the database can be displayed in the Windows window. However, the buttons on the form have to be changed this time, And ListView is used this time.

Let's take a look at the specific code and running results.

Private void Form1_Load (object sender, EventArgs e) {FillListView () ;}/// <summary> /// fill the table view /// </summary> private void FillListView () {string SQL = "select * from Friends"; string connString = @ "server = (local); database = SchoolDB; uid = sa; pwd = 514420 ;"; sqlConnection connection = new SqlConnection (connString); SqlCommand command = connection. createCommand (); connection. open (); command. commandText = SQL; // data table dt able dt = new DataTable (); // SQL data configurator SqlDataAdapter sda = new SqlDataAdapter (command ); // There is a Fill method in the configurator to Fill sda. fill (dt); // use the method of looping to Fill each row of data rows.) foreach (DataRow row in dt. rows) {// Add all fields in the database to the window int id = (int) row ["id"]; string name = (string) row ["name"]; bool sex = (bool) row ["sex"]; DateTime birthday = (DateTime) row ["birthday"]; decimal salary = (decimal) row ["salary"]; // Add the content value of each row to the window ListViewItem lvi = new ListViewItem (); lvi. text = id. toString (); lvi. subItems. add (name); lvi. subItems. add (sex. toString (); lvi. subItems. add (birthday. toString (); lvi. subItems. add (salary. toString (); this. lview. items. add (lvi);} connection. close ();}

Running result:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1933332M9-1.png "title =" Capture. PNG "alt =" 121953764.png"/>

The most important thing is to add the column names in the database to the window, and then add the values one by one. When will this happen...

At the very beginning, this level has been achieved, and the progress has been improved slowly!

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1933335Y8-2.gif "alt =" j_0047.gif "/> Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1933335Y8-2.gif "alt =" j_0047.gif "/>

This article is from the "Ajax girl" blog!

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.