当前位置:编程学习 > C#/ASP.NET >>

SqlCommandBuilder问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();

        private void Form1_Load(object sender, EventArgs e)
        {
            load();
        }
        //窗体加载显示数据
        public void load()
        {
            string cnt = "data source=.; initial catalog =ly; user id =sa; pwd =123";
            SqlConnection conn = new SqlConnection(cnt);
            string sql = "select * from table1";
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        //点击按钮更新
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认修改吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (result == DialogResult.Yes)
            {
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                sda.Update(ds, "table1");
                //MessageBox.Show("修改成功啥的");
            }
        }
    }
} --------------------编程问答-------------------- 问题是什么? --------------------编程问答-------------------- 这是神马意思 --------------------编程问答-------------------- 抱歉哦, 忘记说了, datagridview显示出来了数据, 单元格是可编辑的! 我修改了数据, 然后点按钮保存修改,  有错误!!


错误提示是:Update 无法找到TableMapping['table']或DataTable"table1" --------------------编程问答-------------------- 大神帮忙看看是哪里出问题了,    学习中~~~ --------------------编程问答--------------------
引用 3 楼  的回复:
抱歉哦, 忘记说了, datagridview显示出来了数据, 单元格是可编辑的! 我修改了数据, 然后点按钮保存修改,  有错误!!


错误提示是:Update 无法找到TableMapping['table']或DataTable"table1"

内存中并不存在table1,所以之前你得指定一下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  DataSet ds = new DataSet();
  SqlDataAdapter sda = new SqlDataAdapter();

  private void Form1_Load(object sender, EventArgs e)
  {
  load();
  }
  //窗体加载显示数据
  public void load()
  {
  string cnt = "data source=.; initial catalog =ly; user id =sa; pwd =123";
  SqlConnection conn = new SqlConnection(cnt);
  string sql = "select * from table1";
  conn.Open();
  SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
  sda.Fill(ds,"table1"); //指定一个别名table1
  this.dataGridView1.DataSource = ds.Tables[0];
  }

  //点击按钮更新
  private void button1_Click(object sender, EventArgs e)
  {
  DialogResult result = MessageBox.Show("确认修改吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  if (result == DialogResult.Yes)
  {
  SqlCommandBuilder scb = new SqlCommandBuilder(sda);
  sda.Update(ds, "table1"); //这里才可以找到的
  //MessageBox.Show("修改成功啥的");
  }
  }
  }
} --------------------编程问答-------------------- 绑定时指定table名字,试试
 this.dataGridView1.DataSource = ds.Tables["table1"];
--------------------编程问答--------------------
引用 6 楼  的回复:
绑定时指定table名字,试试
 this.dataGridView1.DataSource = ds.Tables["table1"];

好点的写法
sda.Fill(ds, "table1");
//以下更改DataTabe中的数据
DataTable dt = ds.Tables["table1"];
this.dataGridView1.DataSource = dt; --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  DataSet ds = new DataSet();
  SqlDataAdapter sda = new SqlDataAdapter();

  private void Form1_Load(object sender, EventArgs e)
  {
  load();
  }
  //窗体加载显示数据
  public void load()
  {
  string cnt = "data source=.; initial catalog =ly; user id =sa; pwd =123";
  SqlConnection conn = new SqlConnection(cnt);
  string sql = "select * from table1";
  conn.Open();
  SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
  sda.Fill(ds, "table1"); //需要指定一下别名
  this.dataGridView1.DataSource = ds.Tables[0];
  }

  //点击按钮更新
  private void button1_Click(object sender, EventArgs e)
  {
  DialogResult result = MessageBox.Show("确认修改吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  if (result == DialogResult.Yes)
  {
  SqlCommandBuilder scb = new SqlCommandBuilder(sda);
  sda.Update(ds, "table1");
  //MessageBox.Show("修改成功啥的");
  }
  }
  }
} --------------------编程问答-------------------- 奇怪,我5楼的回答哪去了??
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,