Add and update execl to Asp.net

Source: Internet
Author: 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 ('liao ', 'haibing ')";
Cmd. executenonquery ();

Cmd. commandtext = "create table mytable2 (name char (255), address 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.txt firstname. text. trim () + "','" + txtlastname. text. trim () + "')";
This. update_insert_delete_operator (executestring );
}

Protected void btnupdateselect_click (Object sender, eventargs E)
......{
Updateselect(this.txt firstname. Text. Trim (), this.txt lastname. 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 );
}
/** // The deletion operation cannot be performed, prompting that isam does not support data deletion in the chain table. I wonder if there is any way to solve this problem.
Protected void btndelete_click (Object sender, eventargs E)
......{
Delete(this.txt firstname. 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 ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.