asp.net Execl的添加,更新操作实现代码
程序员文章站
2022-06-21 14:24:54
using system; using system.data; using system.configuration; using system.collections;...
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.oledb;
public partial class html_test_execl : system.web.ui.page
......{
static string connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" + httpcontext.current.server.mappath("~/html/") + "new.xls;extended properties=excel 8.0;";
protected void page_load(object sender, eventargs e)
......{
}
protected void btnadonet_createexecle_click(object sender, eventargs e)
......{
oledbconnection cn = new oledbconnection( connectionstring );
cn.open();
oledbcommand cmd = new oledbcommand();
cmd.connection = cn;
cmd.commandtext = "create table mytable(firstname char(255),lastname char(255))";
cmd.executenonquery();
cmd.commandtext = "insert into mytable (firstname,lastname) values('liao','haibing')";
cmd.executenonquery();
cmd.commandtext = "insert into mytable(firstname,lastname) values('廖','海兵')";
cmd.executenonquery();
cmd.commandtext = "create table mytable2(姓名 char(255) , 住址 char(255))";
cmd.executenonquery();
cn.close();
}
protected void btnshowexecl_content_click(object sender, eventargs e)
......{
showexeclcontent();
}
private void showexeclcontent()
......{
oledbconnection cn = new oledbconnection(connectionstring);
oledbdataadapter dda = new oledbdataadapter("select * from [mytable]", cn);
dataset ds = new dataset();
dda.fill(ds, "mytable");
datagrid1.datasource = ds.tables["mytable"].defaultview;
datagrid1.databind();
}
protected void btninsertexecl_click(object sender, eventargs e)
......{
insertexecldate();
showexeclcontent();
}
private void insertexecldate()
......{
string executestring = "insert into mytable(firstname,lastname)values('" + this.txtfirstname.text.trim() + "','" + txtlastname.text.trim() + "')";
this.update_insert_delete_operator(executestring);
}
protected void btnupdateselect_click(object sender, eventargs e)
......{
updateselect(this.txtfirstname.text.trim(), this.txtlastname.text.trim());
showexeclcontent();
}
private void updateselect(string firstname,string lastname)
......{
string executestring = "update mytable set lastname = '" + lastname + "' where firstname = '" + firstname + "'";
this.update_insert_delete_operator(executestring);
}
/**//**//**////删除操作进行不了,提示isam 不支持在链接表中删除数据。 不知道有没有什么办法可以解决这个问题
protected void btndelete_click(object sender, eventargs e)
......{
delete(this.txtfirstname.text.trim());
showexeclcontent();
}
private void delete(string firstname)
......{
string executestring = "delete from mytable where firstname = '" + firstname + "'";
this.update_insert_delete_operator(executestring);
}
private void update_insert_delete_operator(string executestring)
......{
oledbconnection cn = new oledbconnection(connectionstring);
cn.open();
oledbcommand cmd = new oledbcommand(executestring, cn);
cmd.executenonquery();
cn.close();
}
}
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.oledb;
public partial class html_test_execl : system.web.ui.page
......{
static string connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" + httpcontext.current.server.mappath("~/html/") + "new.xls;extended properties=excel 8.0;";
protected void page_load(object sender, eventargs e)
......{
}
protected void btnadonet_createexecle_click(object sender, eventargs e)
......{
oledbconnection cn = new oledbconnection( connectionstring );
cn.open();
oledbcommand cmd = new oledbcommand();
cmd.connection = cn;
cmd.commandtext = "create table mytable(firstname char(255),lastname char(255))";
cmd.executenonquery();
cmd.commandtext = "insert into mytable (firstname,lastname) values('liao','haibing')";
cmd.executenonquery();
cmd.commandtext = "insert into mytable(firstname,lastname) values('廖','海兵')";
cmd.executenonquery();
cmd.commandtext = "create table mytable2(姓名 char(255) , 住址 char(255))";
cmd.executenonquery();
cn.close();
}
protected void btnshowexecl_content_click(object sender, eventargs e)
......{
showexeclcontent();
}
private void showexeclcontent()
......{
oledbconnection cn = new oledbconnection(connectionstring);
oledbdataadapter dda = new oledbdataadapter("select * from [mytable]", cn);
dataset ds = new dataset();
dda.fill(ds, "mytable");
datagrid1.datasource = ds.tables["mytable"].defaultview;
datagrid1.databind();
}
protected void btninsertexecl_click(object sender, eventargs e)
......{
insertexecldate();
showexeclcontent();
}
private void insertexecldate()
......{
string executestring = "insert into mytable(firstname,lastname)values('" + this.txtfirstname.text.trim() + "','" + txtlastname.text.trim() + "')";
this.update_insert_delete_operator(executestring);
}
protected void btnupdateselect_click(object sender, eventargs e)
......{
updateselect(this.txtfirstname.text.trim(), this.txtlastname.text.trim());
showexeclcontent();
}
private void updateselect(string firstname,string lastname)
......{
string executestring = "update mytable set lastname = '" + lastname + "' where firstname = '" + firstname + "'";
this.update_insert_delete_operator(executestring);
}
/**//**//**////删除操作进行不了,提示isam 不支持在链接表中删除数据。 不知道有没有什么办法可以解决这个问题
protected void btndelete_click(object sender, eventargs e)
......{
delete(this.txtfirstname.text.trim());
showexeclcontent();
}
private void delete(string firstname)
......{
string executestring = "delete from mytable where firstname = '" + firstname + "'";
this.update_insert_delete_operator(executestring);
}
private void update_insert_delete_operator(string executestring)
......{
oledbconnection cn = new oledbconnection(connectionstring);
cn.open();
oledbcommand cmd = new oledbcommand(executestring, cn);
cmd.executenonquery();
cn.close();
}
}