Insert an access instance!

Source: Internet
Author: User

1. Class1.cs

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Data. OleDb;
Using System. Data;

Namespace Mane
{
Public class Class1
{
Public DataTable selectTable (string sqltext, OleDbConnection con)
{
DataTable dt = new DataTable ();
OleDbDataAdapter adp = new OleDbDataAdapter (sqltext, con );
Adp. Fill (dt );
Return dt;
}

Public void selectcmd (string sqltext, OleDbConnection con)
{
OleDbCommand cmd = con. CreateCommand ();
Cmd. CommandText = sqltext;
Try
{
Con. Open ();
Cmd. ExecuteNonQuery ();
}
Catch (Exception e)
{
Throw new SystemException (e. Message );
}
Finally
{
Con. Close ();
}

}
}
}

 

2. news_add.aspx.cs

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Data. oledb;
Using mane;
Using system. Data;

Namespace mane. Admin
{
Public partial class webform2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

Protected void button#click (object sender, EventArgs e)
{
If (! Request. IsAuthenticated)
{
Response. Redirect (Server. MapPath ("Login_Admin.aspx "));
}
Else
{
// Add
Class1 Cl = new class1 ();
Oledbconnection con = new oledbconnection (@ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + @ server. mappath ("/app_data/db1.mdb "));
String sqlstr = "insert into news (title, author, body, date) values ('" + textbox1.text. Trim () + "', '" + textbox2.text. Trim () +
"','" + Freetextbox1.text + "','" + datetime. Now. tow.datestring () + "')";
Cl. selectcmd (Sqlstr, con );
// Refresh
System. Web. HttpContext. Current. Response. Write ("<script language = javascript> alert ('success! '); </Script> ");
}
}
}
}

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.