问题描述
公司让我写个抽奖的小程序,我打算用access座数据库,可是代码都是对的,运行也没有任何错误,就是写不到数据库里面去,但是可以查询。代码如下,为了偷懒用的commandbuilder。直接用oledbcommand也写不进去是过了,烦了我两天这问题,求大虾们帮忙OleDbConnectionconn=newOleDbConnection(ConfigurationSettings.AppSettings["AccessConnString"]);OleDbCommandcomm=newOleDbCommand("select*fromprizeConfig",conn);OleDbDataAdapteradapter=newOleDbDataAdapter(comm);OleDbCommandBuilderbuilder=newOleDbCommandBuilder(adapter);DataSetds=newDataSet();adapter.Fill(ds);DataRowdr=ds.Tables[0].NewRow();dr["prizeName"]="aa";ds.Tables[0].Rows.Add(dr);adapter.Update(ds);
解决方案
解决方案二:
顶上去,怎么没有人再阿??
解决方案三:
你试试添加适配器的插入命令adapter.insertCommand="insertinto.....";
解决方案四:
OleDbCommandInsertCommand=newOleDbCommand();InsertCommand="insertinto....";//...省略InsertCommand的属性设置,你去查一下资料插入的参数怎么设置,问题应该就出在这了adapter.InsertCommand=InsertCommand;
解决方案五:
xxx
解决方案六:
数据库没主键,搞个主键就OK
解决方案七:
你不用DataSet来更新数据,直接用适配器的insertCommand命令很方便的,给你个实例看看,不过是sqlserver的,你把Sql改成OleDb就行了,原理一个样SqlConnectionconn=newSqlConnection(strConn);conn.Open();SqlDataAdapteradp=newSqlDataAdapter();//创建一个往MSSQL里插入数据的Command,并赋给AdapterSqlCommandcmd1=newSqlCommand("InsertintotblEntity(Entity_Id,Entity_Name)Values(@Entity_Id,@Entity_Name)",conn);adp.InsertCommand=cmd1;//设置Insert命令的参数与相应的字段,如下:@Entity_Id是参数,Entity_Id是DataSet中的字段名adp.InsertCommand.Parameters.Add("@Entity_Id",SqlDbType.Char,10,"Entity_Id");adp.InsertCommand.Parameters.Add("@Entity_Name",SqlDbType.VarChar,50,"Entity_Name");//执行UPDATE,更新数据adp.Update(ds1);
解决方案八:
利用Access数据库的时候,连接字符串部分的设置一定要包含mode=readwrite属性部分,默认是readonly.这也是能够查询不能够更新、删除、插入的问题所在,希望能够帮助你
解决方案九:
//请细看OleDbConnectionconn=newOleDbConnection(ConfigurationSettings.AppSettings["AccessConnString"]);//创建连接OleDbDataAdapteradapter=newOleDbDataAdapter("select*fromprizeConfig",conn);//创建adapterDataSetds=newDataSet();//创建DataSet对象adapter.Fill(ds);//把查询数据填充到数据集ds中DataRowdr=ds.Tables[0].NewRow();dr["prizeName"]="aa";ds.Tables[0].Rows.Add(dr);//更新OleDBCommandBuilderbuilder=newOleDBCommandBuilder(adapter);adapter.Update(ds);
解决方案十:
publicstaticreadonlystringDBDriver="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=";privatestaticstringDBConnectionString=(DBDriver+access全路径+access文件名);publicstaticvoidDoSql(stringsql){OleDbConnectionconnection=newOleDbConnection();connection.ConnectionString=DBConnectionString;connection.Open();newOleDbCommand(sql,connection).ExecuteNonQuery();connection.Close();}sql放什么就执行什么!不返回结果!使用DDL