asp 兩種方法串連sql sever 並顯示

來源:互聯網
上載者:User

標籤:bubuko   info   ase   htm   server   ever   工具   cli   xxx   

111 (

第一種、用vs內建的資料連線工具。

1.先建立一個空白的aspx頁面,在工具箱那裡找到“資料--GridView”添加一個GridView 。添加完右邊有個小箭頭,點進去建立資料來源--建立串連

 

伺服器名就是登陸sqlsever介面時的那個伺服器名, 下面選windows身份認證或sql sever身份認證都可以,不過windows好像是有限制,

在選擇資料庫名稱,然後一直下一步,

 這裡要指定好是哪一個表,

然後就可以了。

 

 

第二種、用代碼配置,

1、首先建立一個新的aspx頁面,在對應的.cs那個檔案的命名空間裡要添加一些引用,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;

 

然後再page_load裡面添加這些代碼

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "server=xxxx;database=xxx;uid=user;pwd=123";

string SqlStr = "select *from jwdtable";

SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
DataSet ds=new DataSet();

da.Fill(ds,"jwdtable");

Response.Write(ds);

DataList1.DataSource = ds;
//把資料繫結到datalist1控制項
DataList1.DataBind();

}

 

再在aspx頁面加一個DataList

選擇編輯模板, 可以先輸入一些名稱項,然後在裡面添加label 用來綁定資料庫裡面的列名

添加label後點右邊的箭頭,選擇DataBindings

代碼錶達式那裡填Eval("資料庫裡面的列名")

 

 

 

asp 兩種方法串連sql sever 並顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.