总算是给难产出来了 一下子想到做母亲真难 呵呵 每次在实验室把自己的事做完后 就一个人在哪里整点自己的东西 合起来差不多用了我一下午 code有点长 我就按窗口顺序贴出来吧 希望能对你有用 经测试OK!
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();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
f4.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form5 f5 = new Form5();
f5.Show();
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
add();
}
public void add()
{
SqlConnection sqlConn = null;
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
String sexValue = "";
if(radioButton1.Checked == true)
{
sexValue = radioButton1.Text;
}
if (radioButton2.Checked == true)
{
sexValue = radioButton2.Text;
}
SqlCommand sqlCmd = new SqlCommand("insert into student values('"+ textBox1.Text +"','" + textBox2.Text +"','"+sexValue+"','"+Convert.ToInt16(textBox3.Text)+"','"+textBox4.Text+"')",sqlConn);
sqlCmd.ExecuteNonQuery();
sqlCmd.Dispose();
textBox5.Text = "添加成功!";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.Text = "男";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
radioButton2.Text = "女";
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
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 Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
public void delete()
{
SqlConnection sqlConn = null;
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
if (textBox1.Text != "")
{
string queryString2 = "Delete student where number = '";
string searchNumberValue2 = textBox1.Text;
queryString2 += searchNumberValue2 + "'";
SqlCommand sqlCommand2 = new SqlCommand(queryString2, sqlConn);
sqlCommand2.ExecuteNonQuery();
}
if (textBox2.Text != "")
{
string queryString1 = "Delete student where name = '";
string searchNameValue1 = textBox2.Text;
queryString1 += searchNameValue1 + "'";
SqlCommand sqlCommand1 = new SqlCommand(queryString1, sqlConn);
sqlCommand1.ExecuteNonQuery();
}
textBox3.Text = "删除成功!";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
delete();
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
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 Form4 : Form
{
public Form4()
{
InitializeComponent();
}
public void quireAll()
{
SqlConnection sqlConn = null;
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
string queryString = "Select * from student ";
SqlCommand sqlCommand = new SqlCommand(queryString, sqlConn);
SqlDataReader read = sqlCommand.ExecuteReader();
while (read.Read())
{
listBox1.Items.Add(Convert.ToString(read["number"]) + "" + Convert.ToString(read["name"]) + "" + Convert.ToString(read["sex"]) + "" + Convert.ToString(read["age"]) + " " + Convert.ToString(read["special"]));
}
read.Close();
textBox2.Text = "查询成功!";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
}
}
public void quire()
{
SqlConnection sqlConn = null;
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
string queryString1 = "Select * from student where number = '";
string searchNumberValue1 = textBox1.Text;
queryString1 += searchNumberValue1 + "'";
SqlCommand sqlCommand1 = new SqlCommand(queryString1, sqlConn);
SqlDataReader read1 = sqlCommand1.ExecuteReader();
while (read1.Read())
{
listBox1.Items.Add("学号为:"+read1["number"]);
listBox1.Items.Add("姓名为:"+read1["name"]);
listBox1.Items.Add("性别为:"+read1["sex"]);
listBox1.Items.Add("年龄为:"+read1["age"]);
listBox1.Items.Add("系别为:"+read1["special"]);
}
read1.Close();
textBox2.Text = "查询成功!";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
quire();
}
private void button2_Click(object sender, EventArgs e)
{
quireAll();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
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 Form5 : Form
{
public Form5()
{
InitializeComponent();
}
public void quire()
{
SqlConnection sqlConn = new SqlConnection();
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
if (textBox1.Text != "")
{
string queryString1 = "Select * from student where number = '";
string searchNumberValue1 = textBox1.Text;
queryString1 += searchNumberValue1 + "'";
SqlCommand sqlCommand1 = new SqlCommand(queryString1, sqlConn);
SqlDataReader read1 = sqlCommand1.ExecuteReader();
while (read1.Read())
{
textBox3.Text = Convert.ToString(read1["number"]);
textBox4.Text = Convert.ToString(read1["name"]);
textBox5.Text = Convert.ToString(read1["sex"]);
textBox6.Text = Convert.ToString(read1["age"]);
textBox7.Text = Convert.ToString(read1["special"]);
}
read1.Close();
}
if (textBox2.Text != "")
{
string queryString2 = "Select * from student where name = '";
string searchNameValue2 = textBox2.Text;
queryString2 += searchNameValue2 + "'";
SqlCommand sqlCommand2 = new SqlCommand(queryString2, sqlConn);
SqlDataReader read2 = sqlCommand2.ExecuteReader();
while (read2.Read())
{
textBox3.Text = Convert.ToString(read2["number"]);
textBox4.Text = Convert.ToString(read2["name"]);
textBox5.Text = Convert.ToString(read2["sex"]);
textBox6.Text = Convert.ToString(read2["age"]);
textBox7.Text = Convert.ToString(read2["special"]);
}
read2.Close();
}
textBox8.Text = "查询成功!";
}
catch (Exception ex)
{
Console.WriteLine("连接失败!");
Console.WriteLine("错误为:" + ex.Message);
}
finally
{
sqlConn.Close();
Console.WriteLine("连接已关闭!");
}
}
public void replace()
{
SqlConnection sqlConn = new SqlConnection();
try
{
sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\WindowsFormsApplication1\\WindowsFormsApplication1\\DB\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand("insert into student values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + Convert.ToInt16(textBox6.Text) + "','" + textBox7.Text + "')", sqlConn);
sqlCmd.ExecuteNonQuery();
sqlCmd.Dispose();
textBox8.Text = "更新成功!";
}
catch (Exception ex)
{
Console.WriteLine("连接失败!");
Console.WriteLine("错误为:" + ex.Message);
}
finally
{
sqlConn.Close();
Console.WriteLine("连接已关闭!");
}
}
private void button1_Click(object sender, EventArgs e)
{
quire();
}
private void button2_Click(object sender, EventArgs e)
{
replace();
}
}
}
接下来我们就来看程序的测试 我就上传一些我的操作截图吧
系统主界面
子系统添加界面
子系统删除界面
子系统查询界面
子系统修改信息界面(查询出来后 直接修改你想重新写入的值 如:只想修改学号 在点更新数据按钮进行更新)