DataGridView保存 SQL2000

kenyy5509 2009-03-02 07:21:38
發現在DataGridView控件中修改數據直接連SQL2000數據庫保存時出錯,
但在用SQL2005時沒有問題 .
煩請修改以下DataGridView控件可連接SQL2000修改數據保存的例子.
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlDataAdapter adapter;
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection("server=SQL11;database=db_02;uid=sa;pwd=");
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_tree", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.RowHeadersVisible = false;
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Columns[i].Width = 84;
}
button1.Enabled = false;
dataGridView1.Columns[0].ReadOnly = true;
}
private DataTable dbconn(string strSql)
{
conn.Open();
this.adapter = new SqlDataAdapter(strSql, conn);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
private void button2_Click(object sender, EventArgs e)
{
if (dbUpdate())
{
MessageBox.Show("修改成功!");
}
}
private Boolean dbUpdate()
{
string strSql = "select * from tb_tree";
DataTable dtUpdate = new DataTable();
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear();
DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView1.DataSource;
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);
}
try
{
this.conn.Open();
SqlCommandBuilder CommandBuiler;
CommandBuiler = new SqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
this.conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
dtUpdate.AcceptChanges();
return true;
}
}
}

出錯提示 : 不會傳回任何重要資料行資訊的selectcommand , 不支援updatecommand動態sql的產生 .
...全文
178 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenhu420 2011-08-11
  • 打赏
  • 举报
回复
我感觉用dataadapter.update更新数据库会不会出现并发呢?
要控制并发是不是和一般的逐条保存一样呢?(用锁)
IDtoID 2009-03-03
  • 打赏
  • 举报
回复
来学习的.给1分就好.
tansoft 2009-03-03
  • 打赏
  • 举报
回复
呵呵.分就分吧.
楼上的.你的分这么多还要在乎呀
Dobzhansky 2009-03-02
  • 打赏
  • 举报
回复
把分分了吧
liudeqing2008 2009-03-02
  • 打赏
  • 举报
回复
恩,那就好!除了问题自己先看看,实在不会了再找人帮忙看,这样对你的帮助会更大!
FAFA2008 2009-03-02
  • 打赏
  • 举报
回复
关注
kenyy5509 2009-03-02
  • 打赏
  • 举报
回复
已解決 . 在sql2000表中增加主鍵即可 .

111,126

社区成员

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

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

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