问题描述
我是菜鸟,请各位帮忙解决:我在DataGridView新增数据,直接在单元格中输入,但是不能保存啊。我没有用OracleCommandBuilder,而是用SQL插入,我写了一段小代码请大虾们指点一下。privateconststringSQL_Insert_Student_Info=@"INSERTINTOStudentVALUES(?,?,?)";privateOracleCommandInsertStudentInfo(){OracleCommandcomm=newSqlCommand(SQL_Insert_Student_Info,this.mconnection);OracleParameterCollectionStudentPmC=comm.Parameters;StudentPmC.Add("Gid_New",SqlDbType.VarChar,20,"GID");StudentPmC.Add("Name_New",SqlDbType.VarChar,20,"Name");StudentPmC.Add("Memo_New",SqlDbType.VarChar,20,"Memo");returncomm;}publicboolUpdataStudentInfo(DataSetds){try{OracleDataAdaptersda=newSqlDataAdapter();sda.InsertCommand=InsertStudentInfo();//在此调用前面生成的command对象sda.Update(ds,"Student");returntrue;}catch(Exceptionex){stringstrError=ex.Message;returnfalse;}}
解决方案
解决方案二:
我这个是在里面更新的,你看看能不能用得上,你也可以用这方法添加privatevoiddataGridView2_CellEndEdit(objectsender,DataGridViewCellEventArgse){DBclass.Db_ClassDB=newDBclass.Db_Class();try{stringpriKey=this.dataGridView2["GV_SHOP",e.RowIndex].Value.ToString();stringup_PROPORTION=this.dataGridView2["GV_PROPORTION",e.RowIndex].Value.ToString();stringcmdStr="UPDATEGT_SHOPSCALESETPROPORTION="+up_PROPORTION+"WHERESHOP='"+priKey+"'";System.Data.OracleClient.OracleCommandcmd=newOracleCommand(cmdStr,DB.Conn);DB.Db_Conn();//conn.Open();cmd.ExecuteNonQuery();}catch(OracleExceptionex){MessageBox.Show(ex.Message);}DB.close();}
解决方案三:
你將代碼紅色部分改成綠色部分試一試吧.privateconststringSQL_Insert_Student_Info=@"INSERTINTOStudentVALUES(?,?,?)";privateOracleCommandInsertStudentInfo(){OracleCommandcomm=newSqlCommand(SQL_Insert_Student_Info,this.mconnection);OracleParameterCollectionStudentPmC=comm.Parameters;StudentPmC.Add("Gid_New",SqlDbType.VarChar,20,"GID");StudentPmC.Add("Name_New",SqlDbType.VarChar,20,"Name");StudentPmC.Add("Memo_New",SqlDbType.VarChar,20,"Memo");returncomm;}publicboolUpdataStudentInfo(DataSetds){try{OracleDataAdaptersda=newSqlDataAdapter();sda.InsertCommand=InsertStudentInfo();//在此调用前面生成的command对象sda.Update(ds,"Student");returntrue;mconnection.open();//打開連接;comm.ExecuteNonQuery();}catch(Exceptionex){stringstrError=ex.Message;returnfalse;}mconnection.close();//關閉連接;}
解决方案四:
privatevoidbtnUpdate_Click(objectsender,EventArgse)//更新修改项{try{boolflag=false;OleDbConnectioncn=DataBase.CreateAcc();//建立数据库连接,改成你自己的就可以了。DataTabledt=(DataTable)dgv.DataSource;//dgv:改成你的datagridview的Namefor(inti=0;i<dt.Rows.Count;i++){if(dt.Rows[i].RowState==DataRowState.Modified)//判断datagridview数据行的状态,是否被修改{OleDbDataAdapterda=newOleDbDataAdapter("select*fromStudent",cn);OleDbCommandBuilderbuilder=newOleDbCommandBuilder(da);da.Update(dt);MessageBox.Show("更新成功!");}else{flag=false;}}if(!flag){MessageBox.Show("没有进行修改,无法更新");}}catch(Exceptionex){MessageBox.Show(ex.Message,"错误提示");}}
解决方案五:
你的oracledataadapter应该是在ds之前就有
解决方案六:
zheninchangejiang能不能说的清楚些
解决方案七:
用CommandBuilder对Adapter重新生成命令