SqlCommandBuilder问题

vip5319765 2012-04-07 02:16:05
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("修改成功啥的");
}
}
}
}
...全文
239 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2012-04-07
  • 打赏
  • 举报
回复
奇怪,我5楼的回答哪去了??
暖枫无敌 2012-04-07
  • 打赏
  • 举报
回复
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("修改成功啥的");
}
}
}
}
EnForGrass 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

绑定时指定table名字,试试
this.dataGridView1.DataSource = ds.Tables["table1"];
[/Quote]
好点的写法
sda.Fill(ds, "table1");
//以下更改DataTabe中的数据
DataTable dt = ds.Tables["table1"];
this.dataGridView1.DataSource = dt;
暖枫无敌 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

抱歉哦, 忘记说了, datagridview显示出来了数据, 单元格是可编辑的! 我修改了数据, 然后点按钮保存修改, 有错误!!


错误提示是:Update 无法找到TableMapping['table']或DataTable"table1"
[/Quote]
内存中并不存在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("修改成功啥的");
}
}
}
}
EnForGrass 2012-04-07
  • 打赏
  • 举报
回复
绑定时指定table名字,试试
this.dataGridView1.DataSource = ds.Tables["table1"];
vip5319765 2012-04-07
  • 打赏
  • 举报
回复
大神帮忙看看是哪里出问题了, 学习中~~~
vip5319765 2012-04-07
  • 打赏
  • 举报
回复
抱歉哦, 忘记说了, datagridview显示出来了数据, 单元格是可编辑的! 我修改了数据, 然后点按钮保存修改, 有错误!!


错误提示是:Update 无法找到TableMapping['table']或DataTable"table1"
色拉油 2012-04-07
  • 打赏
  • 举报
回复
这是神马意思
劼哥stone 2012-04-07
  • 打赏
  • 举报
回复
问题是什么?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧