(1)界面
展示数据的是listview列表,其中view的属性是details。列表数据是自动从数据库表中获取到后显示出来的,目前只是实现了添加的功能,其它有时间再写。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 学生数据维护
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection myConnection;
SqlCommand sqlCommand;
//在Form1_Load中的代码已经可以将数据在listview中显示出来了
private void Form1_Load(object sender, EventArgs e)
{
myConnection = new SqlConnection();
//ConnectionString获取用于打开数据库的字符串
myConnection.ConnectionString = "server=localhost;uid=sa;pwd=root;database=xsgl";
// SqlCommand对象用来对SQL Server数据库执行操作命令。
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "select * from student";
myConnection.Open();//打开数据库
//SqlDataReader是个数据读取器,向前不可后退、每次只读取一条。速度快,现代项目里面用它用得最多。
SqlDataReader dreader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);//数据读取器关闭时,连接对象自动关闭。
//表示当下一条数据存在的时候
while(dreader.Read()){
//将id值添加到listview1中。
ListViewItem newItem = listView1.Items.Add(dreader["studID"].ToString().Trim());
newItem.SubItems.Add(dreader["studName"].ToString().Trim());//SubItems获取包含该项的所有子项的集合。
newItem.SubItems.Add(dreader["studSex"].ToString().Trim());//将子项添加到具有指定文本的集合。
newItem.SubItems.Add(dreader["studAddress"].ToString().Trim());
newItem.SubItems.Add(dreader["enterScore"].ToString().Trim());
}
dreader.Close();//关闭读取器
}
//
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0){//如果listview中被选中的数据行数大于0
textBox1.Text = listView1.SelectedItems[0].Text;//选中列表中的id,则会在左边的textbox中显示相应的数据
//SelectedItems表示获取在控件中选定的项,SubItems表示该项的所有子项的集合。
textBox2.Text = listView1.SelectedItems[0].SubItems[1].Text;
textBox3.Text = listView1.SelectedItems[0].SubItems[2].Text;
textBox4.Text = listView1.SelectedItems[0].SubItems[3].Text;
textBox5.Text = listView1.SelectedItems[0].SubItems[4].Text;
}
}
//添加
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "添加")
{
button1.Text = "保存";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox1.Focus();//设置输入焦点
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
button2.Enabled = false;
button3.Enabled = false;
}
else {
sqlCommand.CommandText = "insert into student values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"',"+textBox5.Text+")";
myConnection.Open();//打开数据库
//ExecuteNonQuery方法,执行更新操作,如与 insert、delete 和 update 语句有关的操作,返回的是sql语句所影响的行数,所以用int。
//对于Update、Insert和Delete语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为-1,比如select语句。
int cmdresults = sqlCommand.ExecuteNonQuery();
myConnection.Close();//关闭连接。
//因为添加的数据是一行的,所以这里如果返回的是一,说明执行成功
if (cmdresults == 1)
{
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
button2.Enabled = true;
button3.Enabled = true;
//Items获取包含控件中所有项的集合,Add表示用指定的文本创建一个项并将该项添加到集合中。
ListViewItem newItem = listView1.Items.Add(textBox1.Text);
newItem.SubItems.Add(textBox2.Text);//SubItems获取包含该项的所有子项的集合。
newItem.SubItems.Add(textBox3.Text);//将子项添加到具有指定文本的集合。
newItem.SubItems.Add(textBox4.Text);
newItem.SubItems.Add(textBox5.Text);
button1.Text = "添加";
MessageBox.Show("插入数据成功");
}
else {
MessageBox.Show("失败");
}
}
}
}
}