asp.net Execl的添加,更新操作實現代碼

來源:互聯網
上載者:User

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();
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.