Access 데이터베이스 의 데 이 터 를 SQL Server 에 가 져 오 는 상세 한 방법 인 스 턴 스
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> </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 SQL Server </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 SQL " />
<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 " />
</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();//
con.Open();
DataSet ds = new DataSet(); //
sql = "select * from Score";
OleDbDataAdapter myCommand = new OleDbDataAdapter(sql,con);//
myCommand.Fill(ds, "Score");
myCommand.Dispose();
DataTable DT = ds.Tables["Score"];
con.Close();
myCommand.Dispose();
for (int j = 0; j < DT.Rows.Count; j++)// ACCESS
{
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 ='" + UserName + "'";
if (ExScalar(selsql) > 0)//
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert(' Access SQL !');location='AccessToSQL.aspx';</script>";
}
else
{
string AccessPath = Server.MapPath("UserScore.mdb");// ACCESS
// OPENROWSET OLE DB
sqlstr = "insert into AccessToSQL(ID, , , , )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();//
if (j == DT.Rows.Count - 1)
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert(' .');location='AccessToSQL.aspx';</script>";
}
else
{
Label1.Visible = true;
Label1.Text = "<script language=javascript>alert(' .');location='AccessToSQL.aspx';</script>";
}
conn.Close();
}
}
}
public void AccessLoadData()
{
OleDbConnection myConn = CreateCon();
myConn.Open(); // ,
DataSet myDataSet = new DataSet(); // DataSet
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(' , !');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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access 사용 요약DataSource만 지정하면 됩니다.Data 디렉토리.Asp.net의 이런 방식은 우리로 하여금 상대적인 경로로 데이터베이스 파일의 위치를 지정하는 데 매우 편리하게 할 수 있다.여기서 Provider는 Oledb...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.