.
断开式:
//dgvStudentScore为最下面控件的name属性
//这个表要设置主键
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string conStr = "Data Source=PC-38\\SQLEXPRESS;Initial Catalog=ns;Integrated Security=True";
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from studentscore" , con);
ds = new DataSet();
da.Fill(ds, "studentscore");
DataRow row = ds.Tables["studentscore"].Rows[0];
string s = string.Format("{0}, {1}", row["name"].ToString().Trim(), row["score"]);
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRow r1 = ds.Tables["studentscore"].NewRow();
r1[0] = textBox1.Text;
r1[1] = textBox2.Text;
r1[2] = textBox3.Text;
ds.Tables[0].Rows.Add(r1);
da.Update(ds, "studentscore");
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
private void button3_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRowCollection rows = ds.Tables["studentscore"].Rows;
DataRow row;
for (int i = 0; i < rows.Count; i++)
{
row = rows[i];
if (row["num"].ToString() == textBox1.Text)
{
row["score"] = Int32.Parse(textBox3.Text);
row["name"] = (textBox2.Text);
}
}
dgvStudentScore.DataSource = ds.Tables["studentscore"];
da.Update(ds, "studentscore");
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataRowCollection rows = ds.Tables["studentscore"].Rows;
DataRow row;
for (int i = 0; i < rows.Count; i++)
{
row = rows[i];
if (row["num"].ToString() == textBox1.Text)
{
row.Delete();
}
}
da.Update(ds, "studentscore");
dgvStudentScore.DataSource = ds.Tables["studentscore"];
}
}
}
非断开
//这个表不用设置主键
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int num = 1;
public int sex;
public string conString = "Data Source=.;Initial Catalog=ns;Integrated Security=True";
private void button1_Click(object sender, EventArgs e)
{
num--;
if (num < 1)
{
num = 1;
}
radioButton1.Checked = true;
ShowStudentInfo();
}
private void ShowStudentInfo()
{
SqlConnection conn = new SqlConnection(conString);
conn.Open();
string sql = "select * from stu where num = " + num;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
int sex = reader.GetInt32(2);
textBox1.Text = reader.GetInt32(0).ToString();
textBox2.Text = reader.GetString(1);
if (sex == 1)
// sex值为1是女
{
radioButton1.Checked = false;
radioButton2.Checked = true;
}
reader.Close();
}
conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
num++;
radioButton1.Checked = true;
ShowStudentInfo();
}
private void button3_Click(object sender, EventArgs e)
{
int sex = 1;
if (radioButton2.Checked)
{
sex = 0;
}
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("insert into stu (num,name,sex) values(@num,@name,@sex)", conn);
cmd.Parameters.AddWithValue("@num", textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@sex", sex);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("DELETE from stu where num=@num", conn);
cmd.Parameters.AddWithValue("@num", Convert.ToInt32((textBox1.Text).Trim()));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}