SqlCommandBuild示例

    public partial class Form1 : Form
    {
        private string connString = ConfigurationManager.ConnectionStrings["connstring"].ToString();
        SqlConnection conn ;
        DataSet ds = new DataSet();
        private int actionFlag = 0;
        SqlCommandBuilder scb;
        SqlDataAdapter sda;
        public Form1()
        {
            InitializeComponent();
            LoadData();
            dgvStudent.DataSource = null;
            dgvStudent.DataSource = ds.Tables["student"];
            txtSNO.Text = dgvStudent.Rows[0].Cells[0].Value.ToString();
            txtSName.Text = dgvStudent.Rows[0].Cells[1].Value.ToString();
            groupBox1.Enabled = false;
        }

        private void LoadData()
        {
            conn = new SqlConnection(connString);
            string sql = "select SNO,SName from Student";
            SqlCommand cmd = new SqlCommand(sql, conn);
            sda = new SqlDataAdapter(cmd);
            sda.SelectCommand = cmd;
            //实例化sqlCommandBuild对象
            scb = new SqlCommandBuilder(sda);


            conn.Open();
            sda.Fill(ds, "student");
            conn.Close();
            ds.Tables["student"].PrimaryKey = new DataColumn[] {ds.Tables["student"].Columns["SNO"] };
        }

        private void dgvStudent_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvStudent.Rows.Count == 0) return;
            txtSNO.Text = dgvStudent.CurrentRow.Cells[0].Value.ToString();
            txtSName.Text = dgvStudent.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            actionFlag = 1;
            groupBox1.Enabled = true;
            txtSNO.Text = string.Empty;
            txtSNO.Focus();
            txtSName.Text = string.Empty;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            actionFlag = 2;
            groupBox1.Enabled = true;
            txtSNO.Enabled = false;
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
            switch (actionFlag)
            {
                case 1:
                    DataRow dr = ds.Tables["student"].NewRow();
                    dr["SNO"] = txtSNO.Text.Trim().ToString();
                    dr["SName"] = txtSName.Text.Trim().ToString();
                    ds.Tables["student"].Rows.Add(dr);
                    break;
                case 2:
                     dr = ds.Tables["student"].Rows.Find(txtSNO.Text.Trim());
                    if (dr != null)
                    {
                        dr.BeginEdit();
                        dr["SName"] = txtSName.Text.Trim();
                        dr.EndEdit();
                        txtSNO.Enabled = true;
                    }
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string sno = txtSNO.Text.Trim();
            string sql = "delete from student where SNO=" + sno;
            ds.Tables["student"].Rows.Find(sno).Delete();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //打开连接
            conn.Open();
            //更新到数据库
            sda.Update(ds,"student");
            MessageBox.Show("更新成功!");
            //关闭
            conn.Close();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值