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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
System.Data.OleDb;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
html_Test_Execl : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
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)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
protected void btnADONET_CreateExecle_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cmd.CommandText = "insert into myTable (firstName,lastName) values('liao','haibing')";
cmd.ExecuteNonQuery();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cmd.CommandText = "insert into myTable(firstName,lastName) values('廖','海兵')";
cmd.ExecuteNonQuery();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cmd.CommandText = "create table myTable2(姓名 char(255) , 住址 char(255))";
cmd.ExecuteNonQuery();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cn.Close();
}
protected void btnShowExecl_Content_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ShowExeclContent();
}
private void ShowExeclContent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InsertExeclDate();
ShowExeclContent();
}
private void InsertExeclDate()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "insert into myTable(firstName,lastName)values('" + this.txtFirstName.Text.Trim() + "','" + txtLastName.Text.Trim() + "')";
this.upDate_Insert_Delete_Operator(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected void btnUpdateSelect_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
upDateSelect(this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim());
ShowExeclContent();
}
private void upDateSelect(string firstName,string lastName)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "update myTable set lastName = '" + lastName + "' where firstName = '" + firstName + "'";
this.upDate_Insert_Delete_Operator(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////删除操作进行不了,提示ISAM 不支持在链接表中删除数据。 不知道有没有什么办法可以解决这个问题
protected void btnDelete_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Delete(this.txtFirstName.Text.Trim());
ShowExeclContent();
}
private void Delete(string firstName)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "delete from myTable where firstName = '" + firstName + "'";
this.upDate_Insert_Delete_Operator(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void upDate_Insert_Delete_Operator(string executeString)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
OleDbConnection cn = new OleDbConnection(connectionString);
cn.Open();
OleDbCommand cmd = new OleDbCommand(executeString, cn);
cmd.ExecuteNonQuery();
cn.Close();
}
}
创建一个new.xls,然后再对new.xls进行添加更新操作,在删除时不问题.