[1] Shortcut keys
Toolbox: ctrl+w+x initial position Control range
Properties: F4 or Ctrl+w+p tab jump, home and end also valid
[2] Connection string
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";
[*]
[3]
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 _06 Major Project
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
?
private void btnAdd_Click (object sender, EventArgs e)
{
Get the contents of the text box--to judge (you do)
String name = txtaddname.text;//Name
int gender = Txtaddgender.text = = "Male"? 1:txtaddgender.text = = "female"? 0:2;//standing buried-pit//Tai Hang
Judge whether the value of the sex is 1 or 0 if not, tell the user to enter the error please re-enter (left you)
int ages = Convert.ToInt32 (txtaddage.text);//Age--pit (left for you)
string phone = txtaddphone.text;//phone number
Create a connection string
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";
int count =-1;
Connecting to a database
using (SqlConnection con = new SqlConnection (str))
{
String sql = string. Format ("INSERT into Tblstudent (TSNAME,TSGENDER,TSAGE,TSPHONE,TCLASSID) VALUES (' {0} ', {1},{2}, ' {3} ', {4})", Name, Gender, age, phone, 1);
using (SqlCommand cmd = new SqlCommand (sql, con))
{
Open Database
Con. Open ();
Count = cmd. ExecuteNonQuery ();//Execute SQL statement
}
}
Execute SQL statement
if (Count > 0)
{
MessageBox.Show ("add success");
}
Else
{
MessageBox.Show ("Add failed");
}
Results
}
?
private void Form1_Load (object sender, EventArgs e)
{
Loadallstudent ();//Load Students
?
?
}
?
private void Loadallstudent ()
{
Create a collection that stores each student object
list<student> list = new list<student> ();
Encapsulation into a method---to refresh it.
Querying all the data
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";//Create connection string
Connecting to a database
using (SqlConnection con = new SqlConnection (str))
{
String sql = "Select Tsid, Tsname,tsgender,tsage,tsphone from Tblstudent";
using (SqlCommand cmd = new SqlCommand (sql, con))
{
Con. Open ();//Opening the database
using (SqlDataReader SDA = cmd. ExecuteReader ())
{
if (SDA. HasRows)//If True proves that there is at least one piece of data
{
while (SDA. Read ())//reads to the next bar
{
sda[1]//data for each column
?
Student stu = new Student ();//Create a Student object
Stu. TSId = SDA. GetInt32 (0);//value of primary key ID
Stu. Tsname = sda["Tsname"]. ToString ();
Stu. Tsgender = SDA. Getboolean (2) = = true? ' Male ': ' Female ';
Stu. Tsage = SDA. GetInt32 (3);//Age
Stu. Tsphone = SDA. GetString (4);
List. ADD (Stu);
}
}
}
}
}
?
DGV. DataSource = list;//binds the collection to the control
}
?
private void Deletetsm_click (object sender, EventArgs e)
{
First, determine whether the user selected the row
if (DGV. Selectedrows.count > 0)
{
int count=-1;
String id = DGV. Selectedrows[0]. Cells[0]. Value.tostring ();
Create a connection string
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";
Connecting to a database
using (SqlConnection con=new SqlConnection (str))
{
String sql = "Delete from tblstudent where tsid=" +ID;
using (SqlCommand cmd=new SqlCommand (Sql,con))
{
Con. Open ();
Count =cmd. ExecuteNonQuery ();
?
}//End Using
}//end using
if (count>0)
{
Loadallstudent ();
MessageBox.Show ("delete succeeded");
}
Else
{
MessageBox.Show ("delete failed");
}
?
}//end if
?
?
?
Gets the ID of the selected row
?
MessageBox.Show ("deleted");
}
?
private void Dgv_rowenter (object sender, DataGridViewCellEventArgs e)
{
Whether there is a selected row
if (DGV. SELECTEDROWS.COUNT>0)
{
Get ID Name Gender age
?
Labid.text = DGV. Selectedrows[0]. Cells[0]. Value.tostring (); Value of//id
Name
Txtupname.text = DGV. Selectedrows[0]. CELLS[1]. Value.tostring ();
Gender
Txtupgender.text = DGV. Selectedrows[0]. CELLS[2]. Value.tostring ();
Age
Txtupphone.text = DGV. Selectedrows[0]. CELLS[3]. Value.tostring ();
Txtupage.text = DGV. Selectedrows[0]. CELLS[4]. Value.tostring ();
}
}
?
private void Btnupdate_click (object sender, EventArgs e)
{
int count =-1;
First gets the ID of the currently selected row
string id = labid.text;
int gender = Txtupgender.text = = "Male"? 1:txtupgender.text = = "female"? 0:2;
int gender = Txtupgender.text = = "Male"? 1:0;
Create a string to connect to the database
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";
Connecting to a database
using (SqlConnection con=new SqlConnection (str))
{
?
String sql =string. Format ("Update tblstudent set Tsname= ' {0} ', tsgender={1},tsage={2},tsphone= ' {3} ' where Tsid={4} ', Txtupname.text, GENDER,TXTUPAGE.TEXT,TXTUPPHONE.TEXT,ID);
using (SqlCommand cmd=new SqlCommand (Sql,con))
{
Con. Open (); Open Database
count= cmd. ExecuteNonQuery ();
}
}
if (count>0)
{
Loadallstudent ();//Refresh
MessageBox.Show ("modified successfully");
}
Else
{
MessageBox.Show ("modification failed");
}
?
?
Perform
}
?
private void Btncount_click (object sender, EventArgs e)
{
?
String count = "";
Create a connection string
String str = "Data source=xy-pc;initial catalog=myitcast;integrated security=true";
Connecting to a database
using (SqlConnection con=new SqlConnection (str))
{
String sql = "SELECT COUNT (*) from tblstudent";
using (SqlCommand cmd=new SqlCommand (Sql,con))
{
Con. Open ();//Opening the database
count= cmd. ExecuteScalar (). ToString ();
}
}
Open Database
MessageBox.Show ("A total of" +count+ "Children's Shoes");
Execute SQL statement
}
?
?
}
}
Data to Object
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
?
Namespace _06 Major Project
{
public class Student
{
TSId, Tsname, Tsgender, tsaddress, Tsphone, Tsage, Tsbirthday, Tscardid, Tclassid
Which column to write in
?
Query four columns
?
private int _tsid;
?
public int TSId
{
get {return _tsid;}
set {_tsid = value;}
}
private string _tsname;
?
public string Tsname
{
get {return _tsname;}
set {_tsname = value;}
}
Private char _tsgender; Keng
?
Public Char Tsgender
{
get {return _tsgender;}
set {_tsgender = value;}
}
private string _tsphone;
?
public string Tsphone
{
get {return _tsphone;}
set {_tsphone = value;}
}
private int _tsage;
?
public int Tsage
{
get {return _tsage;}
set {_tsage = value;}
}
?
?
?
}
}
?
20131207-ado. net-16th Day