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