Default.aspx
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "AccessToSQL.aspx.cs" inherits= "Accesstosql"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<style type= "Text/css" >
. style1
{
height:16px;
}
. style3
{
height:23px;
}
</style>
<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
<table align= "Center" border= "1" bordercolor= "honeydew" cellpadding= "0"
cellspacing= "0" >
<tr>
<TD colspan= "2"
Style= "FONT-SIZE:9PT; COLOR: #ffffff; height:16px; Background-color: #ff9933; Text-align:center ">
Write data from an Access database to the SQL Server database </td>
</tr>
<tr>
<TD style= "Background-color: #ffffcc; Text-align:center ">
<asp:gridview id= "GridView2" runat= "Server" cellpadding= "4" forecolor= "#333333"
Gridlines= "None" style= "Font-size:small" width= "331px" >
<footerstyle backcolor= "#990000" font-bold= "True" forecolor= "white"/>
<rowstyle backcolor= "#FFFBD6" forecolor= "#333333"/>
<pagerstyle backcolor= "#FFCC66" forecolor= "#333333" horizontalalign= "Center"/>
<selectedrowstyle backcolor= "#FFCC66" font-bold= "True" forecolor= "Navy"/>
<alternatingrowstyle backcolor= "White"/>
</asp:GridView>
</td>
<TD style= "width:190px; Background-color: #ffffcc; Text-align:center ">
<asp:gridview id= "GridView1" runat= "Server" cellpadding= "4" font-size= "9pt"
Forecolor= "#333333" gridlines= "None" width= "228px" >
<footerstyle backcolor= "#990000" font-bold= "True" forecolor= "white"/>
<rowstyle backcolor= "#FFFBD6" forecolor= "#333333"/>
<selectedrowstyle backcolor= "#FFCC66" font-bold= "True" forecolor= "Navy"/>
<pagerstyle backcolor= "#FFCC66" forecolor= "#333333" horizontalalign= "Center"/>
<alternatingrowstyle backcolor= "White"/>
</asp:GridView>
</td>
</tr>
<tr>
<TD style= "HEIGHT:23PX; Background-color: #ff9900; Text-align:center "
valign= "Top" >
<asp:button id= "Button3" runat= "Server" font-size= "9pt" onclick= "Button1_Click"
text= "Access data to SQL database"/>
<asp:label id= "Label1" runat= "Server" text= "Label" visible= "False"
Style= "Font-size:x-small" ></asp:Label>
</td>
<TD style= "width:190px; height:23px; Background-color: #ff9900; Text-align:center ">
<asp:button id= "Button2" runat= "Server" font-size= "9pt" onclick= "button2_click"
Text= "Show imported data in SQL database"/>
</td>
</tr>
</table>
</form>
</body>
Default.aspx.cs
Copy Code code as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Data.OleDb;
Using System.Data.SqlClient;
public partial class AccessToSQL:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Accessloaddata ();
}
}
Public OleDbConnection Createcon ()
{
String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ("Userscore.mdb") + "; User id=admin; password=; ";
OleDbConnection ODBC = new OleDbConnection (strconn);
return ODBC;
}
Public SqlConnection Createsqlcon ()
{
String Sqlcon = configurationsettings.appsettings["Strcon"];
SqlConnection mycon = new SqlConnection (Sqlcon);
return mycon;
}
protected void Button1_Click (object sender, EventArgs e)
{
String sql = "";
OleDbConnection con = Createcon ();//CREATE DATABASE connection
Con. Open ();
DataSet ds = new DataSet (); To create a dataset
sql = "SELECT * from Score";
OleDbDataAdapter mycommand = new OleDbDataAdapter (Sql,con);//Create Data adapter
Mycommand.fill (ds, "Score");
Mycommand.dispose ();
DataTable DT = ds. tables["Score"];
Con. Close ();
Mycommand.dispose ();
for (int j = 0; J < DT.) Rows.Count; J + +)/loop access data get appropriate information
{
String sqlstr = "";
String ID = DT. Rows[j][0]. ToString ();
String UserName = DT. ROWS[J][1]. ToString ();
String papername = DT. ROWS[J][2]. ToString ();
String userscore = DT. ROWS[J][3]. ToString ();
String examtime = DT. ROWS[J][4]. ToString ();
String selsql = "SELECT count (*) from accesstosql where user name = ' + UserName + '";
if (Exscalar (Selsql) > 0)//Determine if the data has been added
{
Label1.visible = true;
Label1.Text = "<script language=javascript>alert (' The data in this Access database has been imported into the SQL database! '); location= ' accesstosql.aspx ';</script> ';
}
Else
{
String accesspath = Server.MapPath ("Userscore.mdb");//Get Access database path
All connection information required to access remote data in an OLE DB data source by applying the OPENROWSET function
Sqlstr = "INSERT into Accesstosql (ID, user name, quiz paper, score, Test time) Values (' + ID +" ', ' "+ UserName +" ', ' "+ Papername +" ', ' "+" + users Core + "', '" + Examtime + "')";
SQLSTR + + "SELECT * from OPENROWSET" (' microsoft.jet.oledb.4.0 ', ' "+ Accesspath +" '; ') Admin '; ', Score) ';
SqlConnection conn = Createsqlcon ();
Conn. Open ();
SqlCommand mycom = new SqlCommand (SQLSTR, conn);
Mycom. ExecuteNonQuery ()//Add action
if (j = = DT. ROWS.COUNT-1)
{
Label1.visible = true;
Label1.Text = "<script language=javascript>alert (' data import succeeded. '); location= ' accesstosql.aspx ';</script> ';
}
Else
{
Label1.visible = true;
Label1.Text = "<script language=javascript>alert (' data import failed. '); location= ' accesstosql.aspx ';</script> ';
}
Conn. Close ();
}
}
}
public void Accessloaddata ()
{
OleDbConnection myconn = Createcon ();
MyConn.Open (); Open a data link and get a dataset
DataSet myDataSet = new DataSet (); Creating a DataSet Object
String strSQL = "SELECT * from Score";
OleDbDataAdapter mycommand = new OleDbDataAdapter (strSQL, myconn);
Mycommand.fill (myDataSet, "Score");
Gridview2.datasource = myDataSet;
Gridview2.databind ();
Myconn.close ();
}
public int exscalar (String sql)
{
SqlConnection conn = Createsqlcon ();
Conn. Open ();
SqlCommand com = new SqlCommand (SQL, conn);
Return Convert.ToInt32 (COM. ExecuteScalar ());
Conn. Close ();
}
protected void button2_click (object sender, EventArgs e)
{
String sqlstr = "SELECT * from Accesstosql";
SqlConnection conn = Createsqlcon ();
Conn. Open ();
SqlCommand mycom = new SqlCommand (SQLSTR, conn);
SqlDataReader dr = Mycom. ExecuteReader ();
Dr. Read ();
if (Dr. HasRows)
{
GetDataSet (SQLSTR);
}
Else
{
Label1.visible = true;
Label1.Text = "<script language=javascript>alert (' There is no data in the database, please import and query first!") '); location= ' accesstosql.aspx ';</script> ';
}
Dr. Close ();
Conn. Close ();
}
Public DataSet GetDataSet (string sqlstr)
{
SqlConnection conn = Createsqlcon ();
SqlDataAdapter Myda = new SqlDataAdapter (SQLSTR, Conn);
DataSet ds = new DataSet ();
Myda. Fill (DS);
Gridview1.datasource = ds;
Gridview1.databind ();
return DS;
}
}