c#中的学生数据的添加

(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("失败");
                
                }


            
            }

        }
    }
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值