注册 登录
编程论坛 C# 论坛

调试界面数据可以更新,但不能更改数据库里的数据,请大神指点

haining6699 发布于 2016-05-14 19:39, 2346 次点击
程序代码:
using System;
using System.Collections.Generic;
using 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 c1
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private BindingManagerBase dh;
        private SqlCommandBuilder dc;
        private SqlDataAdapter dx;
        private DataSet dq;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strcon = "server=localhost;uid=sa;pwd=123456;database=Test";
           // using (SqlConnection con = new SqlConnection())
           
// {
                con = new SqlConnection();
                con.ConnectionString = strcon;
                con.Open();
                dx = new SqlDataAdapter("select * from dwxx", con);
                dc = new SqlCommandBuilder(dx);
                dq = new DataSet();
                dx.Fill(dq, "dw");
                Binding b1 = new Binding("Text", dq, "dw.单位编号");
                textBox1.DataBindings.Add(b1);
                Binding b2 = new Binding("Text", dq, "dw.单位名称");
                textBox2.DataBindings.Add(b2);
                dataGridView1.DataSource = dq.Tables["dw"];
                dh=this.BindingContext[dq,"dw"];
                comboBox1.DataSource=dq.Tables["dw"];
                comboBox1.DisplayMember = "单位名称";
           // }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dh.Position = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dh.Position < dh.Count - 1) dh.Position++;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dh.Position > 0) dh.Position--;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dh.Position = dh.Count - 1;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dx.Update(dq, "dw");
        }
    }
}

3 回复
#2
haining66992016-05-14 20:31
就没有大神可以指点下吗?真心不知道为什么啊

#3
haining66992016-05-15 08:51
路过的大神,伸一下你的手吧
#4
haining66992016-05-15 09:11
呼叫版主,难道这个帖子应该发在SQL区???
1