P.S. 此範例以Vistual Web Developer 2010 Express內建debug用的IIS為例!! 並未將寫好的aspx複製到OS內自行架設的IIS.


使用工具:
      1. Microsoft IIS 7
      2. Microsoft SQL Server 2008 Express
      3. Microsoft Visual Web Developer 2010 Express





Step 1) 安裝以上的開發工具、IIS與SQL Server


Step 2) 在SQL Server建立一個簡單的Database與Data Table. (我的DataTable只建立兩個欄位id與phoneTel)

            註:由於找不到如何新增資料的工具,所以只好用「文字編輯器」寫一個簡單的資料,欄位與欄位之間用 ":" 隔開,然後在使用「SQL Server匯入和匯出精靈」去將資料匯入


Step 3) (重要) 查看SQL Server的 TCP/IP是否有開啟!!

            「開始」→「所有程式」→「Microsoft SQL Server 2008」→「組態工具」→「SQL Server 組態管理員」→左方的「SQL Server網路組態」→「SQLEXPRESS的通訊協定」→將「TCP/IP」啟用


Step 4) 重新啟動SQL Server Daemon (如:SQL Server Browser.........etc.)


Step 5) 開始寫asp .net去取得SQL Server的資料,我新增一個getData.aspx的項目(檔案),以下則是我提供的asp .net for C# 範例:





getData.aspx檔案: 

-----------------------------------------------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getData.aspx.cs" Inherits="getData" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>  

        <asp:TextBox ID="TextBox1" runat="server" Height="100px" TextMode="MultiLine" Width="200px"></asp:TextBox> 

    </div>
    </form>
</body>
</html>
-----------------------------------------------------------------------------------------------------






getData.aspx.cs                           (藍字為需要修改的)

-----------------------------------------------------------------------------------------------------
using System;
using System.Data.SqlClient;


public partial class getData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = "server=.\\SQLExpress;database=databaseName;User ID=username;Password=password;Trusted_Connection=True;";


        //建立連接
        SqlConnection myConn = new SqlConnection(strConn);


        //打開連接
        myConn.Open();


        String strSQL = @"select * from tableName";


        //建立SQL命令對象
        SqlCommand myCommand = new SqlCommand(strSQL, myConn);


        //得到Data結果集
        SqlDataReader myDataReader = myCommand.ExecuteReader();



        //讀取結果
        while (myDataReader.Read())
        {
            if (myDataReader["id"].ToString() != "")
            {
                TextBox1.Text += myDataReader["id"].ToString();
                TextBox1.Text += " : ";
                TextBox1.Text += myDataReader["phoneTel"].ToString();
                TextBox1.Text += Environment.NewLine;                 //跳行
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------

arrow
arrow
    全站熱搜

    dreamtails 發表在 痞客邦 留言(2) 人氣()