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)文章
.net實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā)實(shí)例代碼
這篇文章主要介紹了.net實(shí)現(xiàn)微信公眾賬號(hào)接口開發(fā)實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
密碼綁定至密碼文本框中(TextMode設(shè)為Password)
一般情況之下TextBox的TextMode設(shè)為Password話,我們想在后臺(tái)(.cs)綁定一個(gè)值至此文本框,是無(wú)法實(shí)現(xiàn)的,如果一定要綁定值的話,該如何實(shí)現(xiàn)呢?,本文將告訴你實(shí)現(xiàn)方法,感興趣的朋友可以參考下2013-01-01
ASP.NET?Core實(shí)現(xiàn)動(dòng)態(tài)獲取文件并下載
這篇文章介紹了ASP.NET?Core實(shí)現(xiàn)動(dòng)態(tài)獲取文件并下載的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
.NET 6開發(fā)TodoList應(yīng)用引入數(shù)據(jù)存儲(chǔ)
這篇文章主要介紹了.NET 6開發(fā)TodoList應(yīng)用引入數(shù)據(jù)存儲(chǔ),本篇文章僅完成了數(shù)據(jù)存儲(chǔ)服務(wù)的配置工作,目前還沒(méi)有添加任何實(shí)體對(duì)象和數(shù)據(jù)庫(kù)表定義,所以暫時(shí)沒(méi)有可視化的驗(yàn)證,僅我們可以運(yùn)行程序看我們的配置是否成功:下面來(lái)看詳細(xì)內(nèi)容吧2021-12-12
asp.net中Session緩存與Cache緩存的區(qū)別分析
實(shí)現(xiàn)數(shù)據(jù)的緩存有很多種方法,有客戶端的Cookie,有服務(wù)器端的Session和Application2013-02-02
增加asp.net應(yīng)用程序性能的20種方法(簡(jiǎn)單有效)
增加asp.net應(yīng)用程序性能的20種方法小結(jié),需要的朋友可以參考下,對(duì)于服務(wù)器也需要一些設(shè)置。2010-01-01
.NET Core 微信小程序退款步驟——(統(tǒng)一退款)
這篇文章主要介紹了.NET Core 微信小程序退款步驟——(統(tǒng)一退款),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫(kù)的方法教程
Influxdb是一個(gè)開源的分布式時(shí)序、時(shí)間和指標(biāo)數(shù)據(jù)庫(kù),使用go語(yǔ)言編寫,無(wú)需外部依賴,下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2018-11-11

