asp.net連接數(shù)據(jù)庫(kù)讀取數(shù)據(jù)示例分享
webconfig配置:
<connectionStrings>
<add name="MSSQL" connectionString="Data Source=localhost;Initial Catalog=test;User ID=sa;password=sa;" providerName="System.Data.SqlClient"/>
</connectionStrings>
前臺(tái)aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DB.aspx.cs" Inherits="DB" %>
<!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>DB</title>
</head>
<body>
<form id="form1" runat="server">
<div>
MS SQL<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
后臺(tái)代碼:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;//記得要using
using System.Configuration;
public partial class DB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsMSSQL = GetDataSet("select * from [Table]", "MSSQL");
this.GridView1.DataSource = dsMSSQL;
this.GridView1.DataBind();
}
protected DataSet GetDataSet(string SqlCommand,string DB)
{
DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[DB].ProviderName);
DbConnection dbConnection = dbProviderFactory.CreateConnection();
dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings[DB].ConnectionString;
DataSet ds = new DataSet();
DbCommand dbCommand = dbProviderFactory.CreateCommand();
dbCommand.Connection = dbConnection;
DbDataAdapter dbDataAdapter = dbProviderFactory.CreateDataAdapter();
dbCommand.CommandText = SqlCommand;
dbDataAdapter.SelectCommand = dbCommand;
dbDataAdapter.Fill(ds);
return ds;
}
}
相關(guān)文章
asp.net core mvc實(shí)現(xiàn)偽靜態(tài)功能
這篇文章主要為大家詳細(xì)介紹了asp.net core mvc實(shí)現(xiàn)偽靜態(tài)功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
詳解ASP.Net Core 中如何借助CSRedis實(shí)現(xiàn)一個(gè)安全高效的分布式鎖
這篇文章主要介紹了ASP.Net Core 中如何借助CSRedis實(shí)現(xiàn)一個(gè)安全高效的分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Asp.net在ashx文件中處理Session問題解決方法
Asp.net在ashx文件中處理Session問題解決方法,需要的朋友可以參考一下2013-05-05
ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限(一)
這篇文章主要介紹了ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限的部分內(nèi)容,后續(xù)我們將繼續(xù)討論這個(gè)話題,希望小伙伴們喜歡。2015-01-01
asp.net中的check與uncheck關(guān)鍵字用法解析
這篇文章主要介紹了asp.net中的check與uncheck關(guān)鍵字用法,以實(shí)例形式較為詳細(xì)的分析了check與uncheck關(guān)鍵字的各種常見用法與使用時(shí)的注意事項(xiàng),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10

