將Access數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入到SQL Server中的詳細(xì)方法實(shí)例


Default.aspx
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<style type="text/css">
.style1
{
height: 16px;
}
.style3
{
height: 23px;
}
</style>
</head>
<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">
將Access數(shù)據(jù)庫(kù)中數(shù)據(jù)寫(xiě)入SQL Server數(shù)據(jù)庫(kù)中</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" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<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" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<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數(shù)據(jù)寫(xiě)入SQL數(shù)據(jù)庫(kù)中" />
<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="SQL數(shù)據(jù)庫(kù)中顯示導(dǎo)入的數(shù)據(jù)" />
</td>
</tr>
</table>
</form>
</body>
</html>
Default.aspx.cs
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();//創(chuàng)建數(shù)據(jù)庫(kù)連接
con.Open();
DataSet ds = new DataSet(); //創(chuàng)建數(shù)據(jù)集
sql = "select * from Score";
OleDbDataAdapter myCommand = new OleDbDataAdapter(sql,con);//創(chuàng)建數(shù)據(jù)適配器
myCommand.Fill(ds, "Score");
myCommand.Dispose();
DataTable DT = ds.Tables["Score"];
con.Close();
myCommand.Dispose();
for (int j = 0; j < DT.Rows.Count; j++)//循環(huán)ACCESS中數(shù)據(jù)獲取相應(yīng)信息
{
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 用戶(hù)姓名='" + UserName + "'";
if (ExScalar(selsql) > 0)//判斷數(shù)據(jù)是否已經(jīng)添加
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('該Access數(shù)據(jù)庫(kù)中數(shù)據(jù)已經(jīng)導(dǎo)入SQL數(shù)據(jù)庫(kù)中!');location='AccessToSQL.aspx';</script>";
}
else
{
string AccessPath = Server.MapPath("UserScore.mdb");//獲取ACCESS數(shù)據(jù)庫(kù)路徑
//應(yīng)用OPENROWSET函數(shù)訪(fǎng)問(wèn) OLE DB 數(shù)據(jù)源中的遠(yuǎn)程數(shù)據(jù)所需的全部連接信息
sqlstr = "insert into AccessToSQL(ID,用戶(hù)姓名,試卷,成績(jī),考試時(shí)間)Values('" + ID + "','" + UserName + "','" + PaperName + "','" + UserScore + "','" + 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();//執(zhí)行添加操作
if (j == DT.Rows.Count - 1)
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('數(shù)據(jù)導(dǎo)入成功.');location='AccessToSQL.aspx';</script>";
}
else
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert('數(shù)據(jù)導(dǎo)入失敗.');location='AccessToSQL.aspx';</script>";
}
conn.Close();
}
}
}
public void AccessLoadData()
{
OleDbConnection myConn = CreateCon();
myConn.Open(); //打開(kāi)數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
DataSet myDataSet = new DataSet(); //創(chuàng)建DataSet對(duì)象
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('數(shù)據(jù)庫(kù)中沒(méi)有數(shù)據(jù)信息,請(qǐng)先導(dǎo)入再查詢(xún)!');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;
}
}
- SQL Server中的數(shù)據(jù)復(fù)制到的Access中的函數(shù)
- Access轉(zhuǎn)Sql Server問(wèn)題 實(shí)例說(shuō)明
- Sql Server、Access數(shù)據(jù)排名的實(shí)現(xiàn)方法(例如:成績(jī)排名)
- SQLServer與Access常用SQL函數(shù)區(qū)別
- SQL 隨機(jī)查詢(xún) 包括(sqlserver,mysql,access等)
- 在A(yíng)CCESS和SQL Server下Like 日期類(lèi)型查詢(xún)區(qū)別
- Access和SQL Server里面的SQL語(yǔ)句的不同之處
- asp.net下Oracle,SQL Server,Access萬(wàn)能數(shù)據(jù)庫(kù)通用類(lèi)
- SQL SERVER 與ACCESS、EXCEL的數(shù)據(jù)轉(zhuǎn)換方法分享
- 八步解決ACCESS自動(dòng)編號(hào)問(wèn)題(將SQL SERVER 2000數(shù)據(jù)庫(kù),轉(zhuǎn)換為ACCESS數(shù)據(jù)庫(kù))
- 將ACCESS數(shù)據(jù)庫(kù)遷移到SQLSERVER數(shù)據(jù)庫(kù)兩種方法(圖文詳解)
- sqlserver,sqlite,access數(shù)據(jù)庫(kù)鏈接字符串整理
- 自己動(dòng)手把ACCESS轉(zhuǎn)換到SQLSERVER的方法
- Access 導(dǎo)入到SQL Server 2005的方法小結(jié)
- JavaScript使用ActiveXObject訪(fǎng)問(wèn)Access和SQL Server數(shù)據(jù)庫(kù)
- SQL Server數(shù)據(jù)復(fù)制到的Access兩步走
相關(guān)文章
在A(yíng)SP.NET Core中顯示自定義的錯(cuò)誤頁(yè)面
大家在用瀏覽器訪(fǎng)問(wèn)服務(wù)器時(shí),不同情況下會(huì)返回不同的信息。服務(wù)器發(fā)生錯(cuò)誤就會(huì)返回錯(cuò)誤信息,我們最熟悉的就是404錯(cuò)誤頁(yè)面,但是這里我想和大家分享下在A(yíng)SP.NET Core中如何顯示自定義的500或404錯(cuò)誤頁(yè)面,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12.NET獲取客戶(hù)端的操作系統(tǒng)版本、瀏覽器版本和IP地址
這篇文章主要為大家詳細(xì)介紹了.NET如何獲取客戶(hù)端的操作系統(tǒng)版本、瀏覽器版本和IP地址,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11ASP.NET通過(guò)自定義函數(shù)實(shí)現(xiàn)對(duì)字符串的大小寫(xiě)切換功能
這篇文章主要介紹了ASP.NET通過(guò)自定義函數(shù)實(shí)現(xiàn)對(duì)字符串的大小寫(xiě)切換功能的方法,給出了兩個(gè)簡(jiǎn)單的實(shí)現(xiàn)方法,涉及asp.net字符串轉(zhuǎn)換操作的相關(guān)技巧,需要的朋友可以參考下2015-07-07asp.net(C#)禁止緩存文件不讓文件緩存到客戶(hù)端
IIS會(huì)按文件地址及參數(shù)將文件緩存到客戶(hù)端,以便再次訪(fǎng)問(wèn)該內(nèi)容時(shí)速度更快,下面為大家介紹C#禁止緩存文件的方法2014-09-09WPF關(guān)鍵幀動(dòng)畫(huà)介紹與實(shí)現(xiàn)
這篇文章介紹了WPF關(guān)鍵幀動(dòng)畫(huà)與實(shí)現(xiàn)方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01asp.net slickupload 使用方法(文件上傳)
asp.net下使用slickupload上傳文件的代碼2009-05-05.Net中異步任務(wù)的取消和監(jiān)控的具體實(shí)現(xiàn)
本文主要介紹了.Net中異步任務(wù)的取消和監(jiān)控的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09asp.net計(jì)算一串?dāng)?shù)字中每個(gè)數(shù)字出現(xiàn)的次數(shù)
計(jì)算一串?dāng)?shù)字中每個(gè)數(shù)字出現(xiàn)的次數(shù),可以這樣子,先判斷輸入的字符串是不是數(shù)字組成,還是否包含有其它字符2012-05-05