sqlDataAdapter 搞不懂了 :(

Doninet 2003-04-25 04:07:10
连接SQL Server 的 Table1

private void b_load_Click(object sender, System.EventArgs e) //载入数据
{
sqlDataAdapter1.Fill (dataSet11);
}

private void update_Click(object sender, System.EventArgs e)//保存修改记录
{
sqlDataAdapter1.Update(dataSet11);
MessageBox.Show ("数据已经更新!");
}

载入数据没有问题,但是修改后 保存时 显示以下错误!!

未处理的“System.InvalidOperationException”类型的异常出现在 system.data.dll 中

其他信息:当传递具有已修改行的 DataRow 集合时,更新要求有效的 UpdateCommand。

...全文
93 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
gbl777 2003-04-25
  • 打赏
  • 举报
回复
打开你的数据库表,里面设置一个主键
Knight94 2003-04-25
  • 打赏
  • 举报
回复
主键,是说你的数据表中有主键设置,如果没有主键的话,那只有象创建InsertCommand一样建一个SqlCommand,付给SqlDataAdapter.UpdateCommand。
Doninet 2003-04-25
  • 打赏
  • 举报
回复
还是搞不懂!
怎么样才能有主键 能详细点吗
Knight94 2003-04-25
  • 打赏
  • 举报
回复

SqlCommandBuilder builder = new SqlCommandBuilder( sqlDataAdapter1 );
放到InitializeComponent中,看是否可以?
asam2183 2003-04-25
  • 打赏
  • 举报
回复
没主键吧,如果你要用CommandBuild的话就得有,除非自己写UPDATE 语句
Doninet 2003-04-25
  • 打赏
  • 举报
回复
求助 求助!!
Doninet 2003-04-25
  • 打赏
  • 举报
回复
up
Doninet 2003-04-25
  • 打赏
  • 举报
回复
在创建sqlDataAdapter1控件 连接SQL Server 时
显示 已经生成update 语句 未能确定唯一标识 “table1”行的列
cnjack 2003-04-25
  • 打赏
  • 举报
回复
表主键没加。
Doninet 2003-04-25
  • 打赏
  • 举报
回复
我是照帮助中的 演练:Windows 窗体中的简单数据访问 示例来做的
yuwen16 2003-04-25
  • 打赏
  • 举报
回复
第一。我觉得可能是表主键没加。
第二就是数据库有问题,具体问题在什么地方?我也不知道。 因为我现在也遇到这问题。

你是否是在生成dataset的时候,提示UpdateCommand生成失败?
Doninet 2003-04-25
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics ;
using System.Data.SqlClient;


namespace 控制
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class formMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button open;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button b_load;
private System.Windows.Forms.Button update;
private System.Windows.Forms.Label label1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private 控制.DataSet1 dataSet11;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public formMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.open = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.b_load = new System.Windows.Forms.Button();
this.update = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataSet11 = new 控制.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// open
//
this.open.Location = new System.Drawing.Point(0, 16);
this.open.Name = "open";
this.open.TabIndex = 0;
this.open.Text = "button1";
this.open.Click += new System.EventHandler(this.open_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.TABLE1;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(312, 128);
this.dataGrid1.TabIndex = 1;
//
// b_load
//
this.b_load.Location = new System.Drawing.Point(104, 16);
this.b_load.Name = "b_load";
this.b_load.TabIndex = 2;
this.b_load.Text = "装入";
this.b_load.Click += new System.EventHandler(this.b_load_Click);
//
// update
//
this.update.Location = new System.Drawing.Point(216, 16);
this.update.Name = "update";
this.update.TabIndex = 3;
this.update.Text = "保存修改";
this.update.Click += new System.EventHandler(this.update_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(336, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "TABLE1", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("id", "id"),
new System.Data.Common.DataColumnMapping("name", "name")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT id, name FROM TABLE1";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO TABLE1(id, name) VALUES (@id, @name); SELECT id, name FROM TABLE1";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.Int, 4, "id"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=top07600\\wanglei;initial catalog=test;password=1;persist security inf" +
"o=True;user id=sa;workstation id=TOP07600;packet size=4096";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
//
// formMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(608, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.update,
this.b_load,
this.dataGrid1,
this.open});
this.Name = "formMain";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new formMain());
}

private void open_Click(object sender, System.EventArgs e)
{
try
{
Process.Start ("inetcpl.cpl");
}
catch(Win32Exception win32ex){
MessageBox.Show ("出错原因!"+win32ex.Message ,"出错",MessageBoxButtons.OK ,MessageBoxIcon.Error );
}
}

private void b_load_Click(object sender, System.EventArgs e)
{
//dataSet11.Clear() ;
//oleDbDataAdapter1.Fill(dsAuthors1);
sqlDataAdapter1.Fill (dataSet11);
}

private void update_Click(object sender, System.EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder( sqlDataAdapter1 );

sqlDataAdapter1.Update(dataSet11);
MessageBox.Show ("数据已经更新!");
}

}
}


全部滴代码 您看看先!!
Knight94 2003-04-25
  • 打赏
  • 举报
回复
没有UpdateCommand,给大家看看如何初始化sqlDataAdapter1代码
htu 2003-04-25
  • 打赏
  • 举报
回复
你有自己写一个UpdateCommand才行
例如
Public Sub CreateSqlDataAdapter()
Dim mySelectText As String = "SELECT * FROM Categories ORDER BY CategoryID"
Dim mySelectConn As String = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"
Dim myDataAdapter As New SqlDataAdapter(mySelectText, mySelectConn)
myDataAdapter.UpdateCommand.CommandText = "UPDATE Categories SET Description='Cheeses, Milk, Ice Cream' WHERE CategoryName='Dairy Products'"
myDataAdapter.UpdateCommand.Connection = CType(myDataAdapter.SelectCommand.Connection, SqlConnection)
End Sub 'CreateSqlDataAdapter
可以参看帮助中的

ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconoverviewofadonet.htm
ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconusingadonetproviderstoaccessdata.htm
Doninet 2003-04-25
  • 打赏
  • 举报
回复
timmy3310(Tim)

加了没用呀 ! (我初学)


jlhdlj()
在调用 Update 之前,必须显式设置这些命令

怎么样设置呢!!
starky 2003-04-25
  • 打赏
  • 举报
回复
nod .

add CommandBuilder:)
ocool 2003-04-25
  • 打赏
  • 举报
回复
我觉的你的存储过程应该有问题,改改试试!
ocool 2003-04-25
  • 打赏
  • 举报
回复
帮你up
timmy3310 2003-04-25
  • 打赏
  • 举报
回复
sqlDataAdapter1.Fill (dataSet11);
之前加一句:

SqlCommandBuilder builder = new SqlCommandBuilder( sqlDataAdapter1 );
jlhdlj 2003-04-25
  • 打赏
  • 举报
回复
DataAdapter 的 Update 方法可调用来将 DataSet 中的更改解析回数据源。与 Fill 方法类似,Update 方法将 DataSet 的实例和可选的 DataTable 对象或 DataTable 名称用作参数。DataSet 实例是包含已作出的更改的 DataSet,而 DataTable 标识从其中检索更改的表。

当调用 Update 方法时,DataAdapter 将分析已作出的更改并执行相应的命令(INSERT、UPDATE 或 DELETE)。当 DataAdapter 遇到对 DataRow 的更改时,它将使用 InsertCommand、UpdateCommand 或 DeleteCommand 来处理该更改。这样,您就可以通过在设计时指定命令语法并在可能时通过使用存储过程来尽量提高 ADO.NET 应用程序的性能。在调用 Update 之前,必须显式设置这些命令。如果调用了 Update 但不存在用于特定更新的相应命令(例如,不存在用于已删除行的 DeleteCommand),则将引发异常。
加载更多回复(2)

110,502

社区成员

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

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

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