问题描述
以下是部分原代码:请各位大侠为小弟解决下.小第在线等待~~~~~privateSqlConnectionconn;privateSqlCommandcomm;privateSqlDataAdapterda;privateDataSetds=newDataSet();privateDataTabledt;privateSqlCommandBuildersqlcommandbuilder;//privateDataRowdr;privatestringstrSql="select会员编号=MemberID,会员卡号=MemberCard,会员姓名=MemberName,会员电话=MemberPhone,会员地址=MemberAddress,累计消费金额=TotalCost,注册日期=RegDatefrom会员表";//连接数据库的方法publicDataTableDataLink(stringstr){conn=newSqlConnection();conn=newSqlConnection();conn.ConnectionString="server=localhost;database=MarketMagSys;integratedsecurity=true;";comm=newSqlCommand(str,conn);da=newSqlDataAdapter();da.SelectCommand=comm;sqlcommandbuilder=newSqlCommandBuilder(da);//da.UpdateCommand=sqlcommandbuilder.GetUpdateCommand();ds.Clear();da.Fill(ds,"MemberInfo");dt=ds.Tables[0];returndt;}//DataGrid控件显示设置publicvoidDataGridTableStyle(){DataGridTableStylets=newDataGridTableStyle();ts.AlternatingBackColor=Color.LightGray;ts.MappingName=dt.TableName;ts.AllowSorting=false;this.dataGrid1.TableStyles.Add(ts);this.dataGrid1.Select(0);}//初始化时绑定数据源privatevoidForm1_Load(objectsender,EventArgse){this.dataGrid1.DataSource=DataLink(strSql).DefaultView;this.dataGrid2.DataSource=DataLink(strSql).DefaultView;DataGridTableStyle();this.txtMemberNum.DataBindings.Add("Text",dt,"会员编号");this.txtMemberCard.DataBindings.Add("Text",dt,"会员卡号");this.txtMemberName.DataBindings.Add("Text",dt,"会员姓名");this.txtMemberPhone.DataBindings.Add("Text",dt,"会员电话");this.txtMemberTotal.DataBindings.Add("Text",dt,"累计消费金额");this.txtMemberAddress.DataBindings.Add("Text",dt,"会员地址");this.dateTimePicker1.DataBindings.Add("Value",dt,"注册日期");cm=(CurrencyManager)BindingContext[dt];}//点击toolBar按钮发生的事件privatevoidtoolBar1_ButtonClick(objectsender,ToolBarButtonClickEventArgse){if(e.Button.Text=="提交修改"){if(this.txtMemberNum.Text.Trim()==""){MessageBox.Show("会员ID不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}if(this.txtMemberCard.Text.Trim()==""){MessageBox.Show("会员卡号不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}if(this.txtMemberTotal.Text.Trim()==""){MessageBox.Show("会员累积消费不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}if(this.txtMemberName.Text.Trim()==""){MessageBox.Show("会员姓名不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}introw=dataGrid1.CurrentCell.RowNumber;this.dataGrid1.CurrentCell=newDataGridCell(row+1,0);cm.EndCurrentEdit();//if(dt.GetChanges()!=null)if(ds.HasChanges()){try{问题所在处da.Update(ds.GetChanges(),"MemberInfo");MessageBox.Show("修改成功","提示",MessageBoxButtons.OK);}catch(Exceptionex){MessageBox.Show(ex.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);this.dt.RejectChanges();}}return;}if(e.Button.Text=="取消修改"){try{cm.CancelCurrentEdit();}catch(Exceptionex){MessageBox.Show(ex.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);}return;}}总是抛出对于不返回任何键列信息的SelectCommand不支持UpdateCommandSQL的动态生成.可我在SQL数据库为表设置主键了.是不是从数据库取出的数据放在DataTable也要设置主键呢.还是其他问题呢.请各帮小弟解决下问题
解决方案
解决方案二:
使用commandbuilder要求数据库有主键且主键信息包含在返回的数据集中查privatestringstrSql="select会员编号=MemberID,会员卡号=MemberCard,会员姓名=MemberName,会员电话=MemberPhone,会员地址=MemberAddress,累计消费金额=TotalCost,注册日期=RegDatefrom会员表";这句sql是否包含了主键信息?
解决方案三:
我在数据库里字段MemberID里设置的是主键
解决方案四:
selectMemberIDas会员编号,MemberCardas会员编号,MemberNameas会员姓名,MemberPhoneas会员电话,MemberAddressas会员地址,TotalCostas累计消费金额,RegDateas注册日期from会员表
解决方案五:
是没看出什么错误,这样试一下da.Update(ds,"MemberInfo");
解决方案六:
啊,我想起来了,好像有一个属性,允许修改设为true试一下