Database lookup, modify, delete

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Data.SqlClient;

Namespace Connsqlform
{
public partial class Form4:form
{
Private SqlConnection Conn;
Private SqlCommand CMD;
Public FORM4 ()
{
InitializeComponent ();
Conn = new SqlConnection ("server=.; Database=snewdata;user=sa;pwd= ");
}

Public DataTable selectstudent ()
{
DataTable dt = new DataTable ();
CMD = Conn.createcommand ();
Query the table
Cmd.commandtext = "Select *from student";
Conn.Open ();
SqlDataReader dr = Cmd.executereader ();
for (int i = 0; i < Dr. FieldCount; i++)
{
Dt. Columns.Add (Dr. GetName (i));
}

while (Dr. Read ())
{
DataRow row = dt. NewRow ();
row["Xuehao"] = dr["Xuehao"];
row["name"] = dr["name"];
row["sex"] = dr["Sex"];
row["Birthday"] = dr["Birthday"];
Dt. Rows.Add (row);
}
Conn.close ();
return DT;
}

private void Button1_Click (object sender, EventArgs e)
{
DataTable dt = Selectstudent ();
ListView1.Columns.Clear ();
LISTVIEW1.COLUMNS.ADD ("School Number");
LISTVIEW1.COLUMNS.ADD ("name");
LISTVIEW1.COLUMNS.ADD ("gender");
LISTVIEW1.COLUMNS.ADD ("Birthday");
ListView1.Items.Clear ();
foreach (DataRow row in dt. Rows)
{
ListViewItem lt = new ListViewItem ();
Lt. Text = row["Xuehao"]. ToString ();
Lt. SubItems.Add (row["name"]. ToString ());
Lt. SubItems.Add (row["sex"). ToString ());
Lt. SubItems.Add (row["Birthday"). ToString ());
LISTVIEW1.ITEMS.ADD (LT);
}
}

private void Listview1_selectedindexchanged (object sender, EventArgs e)
{//The selected index changes when triggered when the SelectedIndexChanged is selected

}

private void Listview1_doubleclick (object sender, EventArgs e)
{
if (ListView1.SelectedItems.Count > 0)
{
String Xuehao = Listview1.selecteditems[0]. Subitems[0]. Text;
String name = Listview1.selecteditems[0]. SUBITEMS[1]. Text;
string sex = Listview1.selecteditems[0]. SUBITEMS[2]. Text;
string birthday = Listview1.selecteditems[0]. SUBITEMS[3]. Text;
Form2 F2 = new Form2 (Xuehao, name, sex, birthday);
F2. Show ();
}
Else
{
MessageBox.Show ("Please select and double click to modify");
}
}

private void Button2_Click (object sender, EventArgs e)
{
if (ListView1.SelectedItems.Count > 0)
{
String Xuehao = Listview1.selecteditems[0]. Subitems[0]. Text;
String name = Listview1.selecteditems[0]. SUBITEMS[1]. Text;
string sex = Listview1.selecteditems[0]. SUBITEMS[2]. Text;
string birthday = Listview1.selecteditems[0]. SUBITEMS[3]. Text;
Form2 F2 = new Form2 (xuehao,name,sex,birthday);
F2. Show ();
}
Else
{
MessageBox.Show ("Please select and click to modify");
}
}

private void Button3_Click (object sender, EventArgs e)
{
if (ListView1.SelectedItems.Count > 0)
{
SqlCommand cmd = Conn.createcommand ();
Cmd.commandtext = "Delete from student where [email protected]";
Cmd. Parameters.clear ();

Cmd. Parameters.Add ("@xuehao", Listview1.selecteditems[0]. Text);
Conn.Open ();
int count = cmd. ExecuteNonQuery ();
if (Count > 0)
{
MessageBox.Show ("Delete succeeded, number of rows affected:" + count.) ToString ());
}
Else
{
MessageBox.Show ("Delete failed! ");
}
Conn.close ();
}
}
}
}

Database lookup, modify, delete

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.