close

方式一:可手動在web.config下新增

方式二:在設計界面拉個SqlDataSource並連結完成

 

<connectionStrings>

       <add name="Connection1" connectionString="Data Source=資料庫主機名稱與位址;Initial Catalog=資料庫名稱;Persist

Security Info=True;User ID=帳號;Password=密碼" providerName="System.Data.SqlClient"/>

</connectionStrings>

 

//匯入命名空間

using System.Data;

using System.Data.SqlClient;

 

//建立連線物件,取得設定在 Web.config 的 connectionStrings

ConnectionStringSettings Connection1 = ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString;

//連結資料庫

SqlConnection conn = new SqlConnection(Connection1);

conn.Open();

//執行SQL指令

string strSQL = "SELECT COUNT(*) FROM TF_SKTZ";

SqlCommand cmd = new SqlCommand(strSQL, conn);

//傳回受影響的資料列數目

cmd.ExecuteNonQuery();

//傳回查詢所傳回的結果集第一個資料列的第一個資料行

int count = (int)cmd.ExecuteScalar();

//關閉資源

cmd.Dispose();

conn.Close();

arrow
arrow
    全站熱搜

    Chang Sunkist 發表在 痞客邦 留言(1) 人氣()