ADO.net中数据更新问题

小明二号 2010-11-23 05:13:47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace 数据更新
{
class Program
{
static void Main(string[] args)
{

SqlConnection thisconn = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=northwind");

thisconn.Open();



SqlDataAdapter thisAdapter=new SqlDataAdapter("SELECT * FROM Tabletest",thisconn);

SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

DataSet thisDataSet = new DataSet();

thisAdapter.Fill(thisDataSet, "Tabletest");



Console.WriteLine("name before change:{0}", thisDataSet.Tables["Tabletest"].Rows[1]["姓名"]);



thisDataSet.Tables["Tabletest"].Rows[1]["姓名"] = "孙六";



thisAdapter.Update(thisDataSet, "Tabletest");

Console.WriteLine("name after change:{0}", thisDataSet.Tables["Tabletest"].Rows[1]["姓名"]);

thisconn.Close();
}
}
}

在vs2008环境下运行代码出现提示:“对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”刚开始学习这方面知识,大哥大姐帮忙解决啊?
...全文
69 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
小明二号 2010-11-24
  • 打赏
  • 举报
回复
终于解决了,表示自己建的,没设主键啊。给表设上主键就Ok了。
小明二号 2010-11-23
  • 打赏
  • 举报
回复
运行时提示“对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”
小明二号 2010-11-23
  • 打赏
  • 举报
回复
thisAdapter.Update(thisDataSet, "Tabletest");
在这一个地方出问题.
xfann 2010-11-23
  • 打赏
  • 举报
回复
看了好像没有什么问题
小明二号 2010-11-23
  • 打赏
  • 举报
回复
我试了,好像不行啊!!
xingheng907 2010-11-23
  • 打赏
  • 举报
回复
貌似好像没有错误吧
TireYang 2010-11-23
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace 数据更新
{
class Program
{
static void Main(string[] args)
{

SqlConnection thisconn = new SqlConnection(@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=northwind");

thisconn.Open();

SqlDataAdapter thisAdapter=new SqlDataAdapter("SELECT * FROM Tabletest",thisconn);

DataSet thisDataSet = new DataSet();

thisAdapter.Fill(thisDataSet, "Tabletest");

SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

thisAdapter.InsertCommand = commandBuilder.GetInsertCommand();
thisAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
thisAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();

Console.WriteLine("name before change:{0}", thisDataSet.Tables["Tabletest"].Rows[1]["姓名"]);
thisDataSet.Tables["Tabletest"].Rows[1]["姓名"] = "孙六";

thisAdapter.Update(thisDataSet, "Tabletest");
Console.WriteLine("name after change:{0}", thisDataSet.Tables["Tabletest"].Rows[1]["姓名"]);
thisconn.Close();
}
}
}


你试试,你少了下面这些。

thisAdapter.InsertCommand = commandBuilder.GetInsertCommand();
thisAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
thisAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
zhangzhen_927116 2010-11-23
  • 打赏
  • 举报
回复
哇靠 。。看不懂

110,502

社区成员

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

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

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