您好,欢迎访问一九零五行业门户网

Access数据库多条数据Insert

由于在此之前我没有用过access数据库的,当需要想数据库中插入多条数据时,我们不妨先按照sql server的做法:insert into tablename(column1,column2) values (a,b),(c,d),(e,f)。于是按照这个思路,我的第一个方案出来了。 尝试一: the demo: stringbuild
由于在此之前我没有用过access数据库的,当需要想数据库中插入多条数据时,,我们不妨先按照sql server的做法:“insert into tablename(column1,column2) values (a,b),(c,d),(e,f)”。于是按照这个思路,我的第一个方案出来了。
尝试一:
the demo:
stringbuilder builist = new stringbuilder(string.format(({0},0), userid));foreach (repeateritem item in rpt_adminrole.items){if (item.itemtype == listitemtype.item || item.itemtype == listitemtype.alternatingitem){htmlinputcheckbox cbrole = item.findcontrol(cb_role) as htmlinputcheckbox;if (cbrole.checked){builist.append(,();builist.append(userid);builist.append(,);builist.append(cbrole.value);builist.append());}}}the dal:
/// /// 添加role关系/// /// 角色关系 eg: (1,1),(1,2)/// public static int insertrolecontact(string rolecontact){string sql = insert into sky_admin_role(adminid,roleid) values +rolecontact;return common.oledbhelper.executenonquery(commandtype.text, sql, null);}exec下就会出现这样的错误:sql 语句的结束位置缺少分号 (;)。
      access对sql的支持果然是大大精简,到此尝试一失败!, 很快在我有另外idea。sql server 多表查询对select table 的支持!我可以直接传一个datatable到sql语句中,说干就干! 
尝试二
the demo : 获取datatable
public datatable getinsertsql(repeater rep,string controlid){datatable data = new datatable();data.columns.add(adminid);data.columns.add(roleid);foreach (repeateritem item in rep.items){if (item.itemtype == listitemtype.item || item.itemtype == listitemtype.alternatingitem){htmlinputcheckbox cb = item.findcontrol(controlid) as htmlinputcheckbox;if (cb.checked){datarow row = data.newrow();row.itemarray = new object[] { userid, cb.value};data.rows.add(row);}}}return data;}the dal:
public static int insertrolecontact(datatable dt){string sql = insert into sky_admin_role(adminid,roleid) select * from @data;oledbparameter[] param = new oledbparameter[] {new oledbparameter(@data,?){value =dt}};return common.oledbhelper.executenonquery(commandtype.text, sql, param);}     当代码到这里我就知道此方法行不通,因为oledbtype中没有对应的table类型,如果是sql server由于支持xml可以设置为 sqldbtype.xml类型来传递datatable数据,由于sql本事对xml的支持 ,可以用sql基于xml的查询,本文主要讨论access,此处暂不讨论了!尝试二宣布失败!。接下来我又想到了零时表,access是不是也支持零时表的查询呢?
尝试三
the demo: 取出我想要的数据格式 (1,2,3)
public string getinsercollection(repeater rep, string controlid){stringbuilder buicollecton = new stringbuilder();buicollecton.append((0);foreach (repeateritem item in rep.items){if (item.itemtype == listitemtype.item || item.itemtype == listitemtype.alternatingitem){htmlinputcheckbox cb = item.findcontrol(controlid) as htmlinputcheckbox;if (cb.checked){buicollecton.append(string.format(,{0}, cb.value));}}}buicollecton.append());return buicollecton.tostring();}
the dal :
public static int insertrolecontact(int userid,string rolecollection)
        {
            stringbuilder buisql = new stringbuilder();
            buisql.append(declare @skycontact table(userid int,roleid int););
            buisql.append(insert into @skycontact values select + userid + ,r_id from sky_role;);
            buisql.append(string.format(insert into sky_admin_role values (select * from @skycontact where roleid in {0}),rolecollection));
            return common.oledbhelper.executenonquery(commandtype.text, buisql.tostring(), null);
        }
      这里模仿sql server中定义一个零时表,然后向其中插入尽可能全的数据,然后在基于零时表查询出想要的数据放入到我想要的数据中执行!exec下结果又出问错了!此处抛出这样的错误:无效的 sql语句;期待 'delete'、'insert'、'procedure'、'select'、或 'update'。其实会出错完全可以想想的到,毕竟access中连insert into table values (1,2),(1,3) 这样的语句都不支持。此时尝试三也不得不宣告失败!尝试了这么多,我不得不使用早就准备用的方法 多条insert一起执行。
尝试四
the demo: 先获取我想要的数据形式 :1,2,3 此处略。看sql:
public static int insertrolecontact2(int userid, string rolecollection){string[] arr = rolecollection.split(',');stringbuilder builsql = new stringbuilder();foreach (string item in arr){builsql.append(string.format(insert into sky_admin_role(adminid,roleid) values ({0},{1});,userid,convert.toint32(item)));}return common.oledbhelper.executenonquery(commandtype.text, builsql.tostring(), null);}
其它类似信息

推荐信息