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