dtab.AcceptChanges(); //它会取消所以修改标识,这样一来,所以修改都不能通过SqlCommandBuilder取得了,所以这句不能要!SqlCommandBuilder会在myDataAdapter.Update之后自动执行AcceptChanges()的!
解决方案 »
- ABC.aspx .Designer.cs , ABC.aspx .cs , ABC.aspx 是如何关联的?
- C#打印 如何获取本地word文档
- 随机生成事件
- datagridview 复选框问题
- DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN 导致javascript 不执行
- 请问在DataGrid里怎么样能改变超级链接列的样式啊?如链接的字体颜色。
- 急求 内有详细说明 在线等候
- 急!急!急救啊,vs.net2005直接打印水晶报表的例子
- type safe 什么意思
- 一个很实用的问题,也许并不高难,盼望解答
- 我想得到毫秒??能行么?
- 100分求13个数字(全由数字3、1或0组成)中连续3,连续1,连续0的个数的算法???
我把dtab.AcceptChanges();去掉了,还是不行
说了不行了。有别的办法没
myDataAdapter.Update(ds, myTableName);
dtab.AcceptChanges();试试dtab.AcceptChanges();放在Update后面
应该是这样,再就是你的表没有主键!!!
不行,还是老样子
michaelowenii(少年狂)
能说细点么,光加
this.BindingContext[ds,myTableName].EndCurrentEdit();
还是再需要加别的?这条加上后出错
System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at ddd.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\administrator\my documents\visual studio projects\ddd\form1.cs:line 149
的确是没有主键,这是为什么呢,怪,请老兄帮忙解释一下好不
前面加上
myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
myDataAdapter.InsertCommand=cb.GetInsertCommand();试试
myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
myDataAdapter.InsertCommand=cb.GetInsertCommand();
应该可以了吧,我比较赞成使用存储过程
.net被封装的东西比较多,myDataAdapter.update就被封装了,根本不知道他里面是怎么执行的,所以还是自己写sql吧
1。去掉dtab.AcceptChanges();
2。在myDataAdapter.Update(ds, myTableName);
前面加上
myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
myDataAdapter.InsertCommand=cb.GetInsertCommand();^_^
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace DemoAcceptChanges
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DemoAcceptChanges : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public DemoAcceptChanges()
{
//
// 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.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 104);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Begin";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 104);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 2;
this.label2.Text = "Resault:";
//
// DemoAcceptChanges
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(280, 157);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.button1,
this.label1});
this.Name = "DemoAcceptChanges";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DemoAcceptChanges());
}
private void RowsAcceptChanges()
{
try
{
string myConnection="server=HI4-SV05;DataBase=pubs;UID=sa;Password=aps;pooling=true";
string mySelectQuery="SELECT * FROM authors";
DataTable dtab= new DataTable();
DataSet ds = new DataSet();
DataRow dr;
SqlConnection myConn = new SqlConnection(myConnection);
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery,myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
//myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
//--open Connection
myConn.Open();
myDataAdapter.Fill(ds,"pubs");
//Make Changes
dtab=ds.Tables["pubs"];
dr=dtab.Rows[0];
dr.BeginEdit();
dr["au_lname"]="UserName";
dr["zip"]="12345";
dr["phone"]="13097777777";
dr.EndEdit();
//dtab.AcceptChanges();
//Update DataBase
myDataAdapter.UpdateCommand=cb.GetUpdateCommand();
myDataAdapter.DeleteCommand=cb.GetDeleteCommand();
myDataAdapter.InsertCommand=cb.GetInsertCommand();
int i=myDataAdapter.Update(ds,"pubs");
//int i=myDataAdapter.UpdateCommand.ExecuteNonQuery();
myConn.Close();
//--end //--Show Resault
if(i>0)
{
this.label1.Text="Success!所影响的行数为:"+i;
}
else
{
this.label1.Text="Sorry!Failed!";
}
//--end Show
}
catch(Exception er)
{
throw er;
}
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.RowsAcceptChanges();
}
catch(Exception er)
{
this.label1.Text=er.Message.Trim();
}
}
public void d()
{
}
}
}