2014年10月30日 星期四

5種資料庫存取

http://anal02.pixnet.net/blog/post/79156925-%5Bvb.net%5D%E6%96%BC5%E7%A8%AE%E8%B3%87%E6%96%99%E5%BA%AB%E5%AD%98%E5%8F%96%E9%97%9C%E9%8D%B5%E7%A8%8B%E5%BC%8F%E7%A2%BC-


[VB.NET]於5種資料庫存取關鍵程式碼

VB.NET於5種資料庫存取關鍵程式碼(以OleDbDataAdapter方法查詢資料表)(以OleDb方法連線)

在網路上找到超好的方法大家可以試試看 . 適用於VB2010

以OleDbDataAdapter方法查詢資料表  以OleDb方法連線

1.  顯示全部資料表內容複製程式碼

在VB開頭前一定要加入這些東西

Imports System.Data
Imports System.Data.OleDb

接下來是連接程式 有ACCESS XLS 很多做法都在下面

       '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

        '查詢資料

        Dim str1 As String = "select * from 資料表"

        Dim adp1 As OleDbDataAdapter = New OleDbDataAdapter(str1, conn)

        '將查詢結果放到記憶體set1上的"1a "表格內

        Dim set1 As DataSet = New DataSet

        adp1.Fill(set1, "1a")

        '將記憶體的資料集合存放到視窗畫面上的DataGrid上

        DataGridView1.DataSource = set1.Tables("1a")



        '關閉資料庫的連結

        conn.Close()

2.  //查詢『某紀錄』複製程式碼
Imports System.Data

Imports System.Data.OleDb

    '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

        '查詢資料

        Dim str1 As String = " Select * from 資料表 where name = 'jack'"

        Dim adp1 As OleDbDataAdapter = New OleDbDataAdapter(str1, conn)

        '將查詢結果放到記憶體set1上的"1a "表格內

        Dim set1 As DataSet = New DataSet

        adp1.Fill(set1, "1a")

        '將記憶體的資料集合存放到視窗畫面上的DataGridView上

        DataGridView1.DataSource = set1.Tables("1a")

        '關閉資料庫的連結

        conn.Close()

注意

查詢
(a).SQL查詢『某紀錄』語法
   Select * from 1a where name = 'jack'
(b).當有變數時的SQL查詢語法(C#)
  "Select * from 資料表1 where PH = '" + TextBox1.Text + " '"
 適用於VB2010作法

3.  //以關鍵字的方式查詢『某紀錄』複製程式碼
Imports System.Data

Imports System.Data.OleDb

    '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

        '查詢資料

        Dim str1 As String = " Select * from 資料表 where name like '%jack%'"

        Dim adp1 As OleDbDataAdapter = New OleDbDataAdapter(str1, conn)

        '將查詢結果放到記憶體set1上的"1a "表格內

        Dim set1 As DataSet = New DataSet

        adp1.Fill(set1, "1a")

        '將記憶體的資料集合存放到視窗畫面上的DataGrid上

        DataGridView1.DataSource = set1.Tables("1a")

        '關閉資料庫的連結

        conn.Close()

注意

查詢
(a).SQL查詢『某紀錄』語法
n   Select * from 1a where name like ‘%jack%’
(b).當有變數時的SQL查詢語法(C#)
n   “Select * from 1a where name like ‘%” + textBox1.text + “%’”
(c).當有變數時的SQL查詢語法(VB.NET)
n   “Select * from 1a where name like ‘%” & textBox1.text & “%’”

4.  //刪除『某紀錄』複製程式碼
Imports System.Data

Imports System.Data.OleDb

        '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

    '刪除資料庫內的記錄

    '設定刪除記錄的 SQL語法 及資料庫執行指令OleDbCommand

      Dim str1 As String = "delete * from 資料表 where name = 'jack'"

        Dim cmd As OleDbCommand = New OleDbCommand(str1, conn)

        '執行資料庫指令OleDbCommand

        cmd.ExecuteNonQuery()

        '關閉資料庫的連結

        conn.Close()

        '顯示成功刪除記錄的訊息()

        MessageBox.Show("成績刪除一筆記錄了", "成功刪除", MessageBoxButtons.OKCancel,MessageBoxIcon.Information)

刪除
(a).SQL刪除『某紀錄』語法
n   delete * from 1a where name = ‘jack’
(b).當有變數時的SQL刪除詢語法(C#)
n   “delete * from 1a where name = ‘” + textBox1.text + “’”
(c).當有變數時的SQL刪除語法(VB.NET)
n   “delete * from 1a where name = ‘” & textBox1.text & “’”

5.  //新增『某紀錄』複製程式碼
Imports System.Data

Imports System.Data.OleDb

        '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

    '新增記錄到資料庫內

    '設定新增記錄的 SQL語法 及資料庫執行指令OleDbCommand

    Dim str1 As String = "Insert Into 資料表(name,chi)Values('jack',90)"

        Dim cmd As OleDbCommand = New OleDbCommand(str1, conn)

        '執行資料庫指令OleDbCommand

        cmd.ExecuteNonQuery()

        '關閉資料庫的連結

        conn.Close()

        '顯示成功新增記錄的訊息()

        MessageBox.Show("成績新增一筆記錄了", "成功新增", MessageBoxButtons.OKCancel,MessageBoxIcon.Information)

新增
(a).SQL新增『某紀錄』語法
n   Insert Into 1a(name,chi)Values(‘jack’,90)
(b).當有變數時的SQL新增語法(C#)
n   “Insert Into 1a(name,chi)Values ‘” + textBoxName.text + “’,” + Int32.Parse(textBoxName.text) + “)”;
(c).當有變數時的SQL新增語法(VB.NET)
n   “Insert Into 1a(name,chi)Values ‘” & textBoxName.text & “’,” & Int32.Parse(textBoxName.text) & “)”;”


6.  //修改『某紀錄』複製程式碼
Imports System.Data

Imports System.Data.OleDb

        '連結資料庫

        '#######  Access 2003 (*.mdb)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.Oledb.4.0;Data source=資料庫名稱.mdb"

        ''#######  Access 2007 (*.accdb)  ##############

        Dim str As String = "Provider=Microsoft.ACE.Oledb.12.0;Data source=資料庫名稱.accdb"

        ''#######  Excel 2003 (*.xlx)  ##############

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=資料庫名稱.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        ''#######  Excel 2008 (*.xlsx)  ##############

        'Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=資料庫名稱.xlsx;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

       '####### 以 Windows 驗證登入帳戶 SQL Server 2005 ######################

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;Integrated Security=SSPI;"

        '####### 以 SQL Server 驗證登入帳戶 SQL Server 2005 (2種方法) ##############

        'Dim str As String = "Provider=sqloledb;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        'Dim str As String = "Provider=SQLNCLI.1;Data Source=伺服器名稱;Initial Catalog=資料庫名稱;User Id=帳戶名稱;Password=密碼;"

        ''#######  txt/csv  ################################

        '注意:若第一列有欄位名稱,則HDR=YES  (預設值為YES)(第一行當欄位名稱處理)

        '注意:若第一列無欄位名稱,直接就是數據了,則HDR=NO (第一行當數據處理)

        'Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;HDR=YES';Data Source=目錄名稱或是|DataDirectory|\"

        Dim conn As OleDbConnection = New OleDbConnection(str)

        conn.Open()

        '修改資料庫內的記錄

        '   設定修改記錄的  SQL語法及資料庫執行指令OleDbCommand

        Dim str1 As String = "Update 資料表 set name = 'jack', chi = 90 where id_no='90001'"

        Dim cmd As OleDbCommand = New OleDbCommand(str1, conn)

        '執行資料庫指令OleDbCommand

        cmd.ExecuteNonQuery()


        '關閉資料庫的連結

        conn.Close()



        '顯示成功修改記錄的訊息()

        MessageBox.Show("成績修改一筆記錄了", "成功修改", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)


修改
(a).SQL修改『某紀錄』語法
n   Update 1a set name = ‘jack’, chi = 90 where id_no=’90001’
(b).當有變數時的SQL修改語法(C#)
n   “Update 1a set name = '" + textBoxName.Text + "', chi = " + Int32.Parse(textBoxChi.Text) + " where id_no=' " + textBoxNo.Text + "'";


B.注意2:什麼時候要用單引號『’』,什麼時候不用加呢


n  字串: 字串前後要加上 ‘ 單引號
 Values('" & txt_5_PrjCode.Text & "')”

n  數值:數值前後要不用加,但是所引用的textbox.text要先用Int32.Parse轉換成數值格式
Values(" & Int32.Parse (txt_5_Period.Text) & "')”

n  日期:字串前後要加上 ‘ 單引號,所引用的textbox.text要先用DateTime.Parse轉換成日期格式
Values('" & DateTime.Parse(txt_5_BeginDay.Text) & "')”


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

補充上集Ch.14--自己撰寫SqlDataSource「新增資料」,並採用參數(InsertParameters)






如何抓取FormView裡的某個欄位,填入預設值

http://vanessa0317.wordpress.com/2010/01/25/%E5%A6%82%E4%BD%95%E6%8A%93%E5%8F%96formview%E8%A3%A1%E7%9A%84%E6%9F%90%E5%80%8B%E6%AC%84%E4%BD%8D%E5%A1%AB%E5%85%A5%E9%A0%90%E8%A8%AD%E5%80%BC/


FormView裡,有些如留言者IP、或是留言時間,不需要自動填入,可以在FormView的DataBound事件中,利用FindControl的方式來取得。
例如:

Protect Sub FormView1_DataBound(ByVal sender as Object, ByVal e As system.EventArgs) Handles FormView1.DataBound
       CType(FormView1.FindControl(”時間欄位”),TextBox).Text=Now
       CType(FormView1.FindControl(”IP欄位”),TextBox).Text=Request.ServerVariables(”REMOTE_ADDR”)
End Sub
另外一種方式,是在Inserting時,再把值代入,這種方法,畫面中不需要用TextBox來顯示。
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
        e.Values.Item(”時間欄位”) = Now
        e.Values.Item(”IP欄位”) = Request.ServerVariables(”REMOTE_ADDR”)
    End Sub

2014年10月27日 星期一

android開發環境建置



資料來源:http://www.inote.tw/how-to-set-android-development-environment-with-android-sdk

■ 軟體名稱:Android SDK
■ 軟體版本:23.0.2
■ 軟體介面:英
■ 軟體性質:免費軟體
■ 支援系統:MAC OS / Windows / Linux
■ Win版下載:http://forum.jeasy.info/viewtopic.php?f=61&t=1578
■ Mac版下載:http://forum.jeasy.info/viewtopic.php?f=62&t=1579
■ 官方網站:http://developer.android.com/sdk/index.html

Step 1.
首先,先安裝好 「JAVA SDK」 後 (Mac OS 有內建 JAVA SDK),再去「Android SDK」下載這一個軟體,下載前請先按下「DOWNLOAD FOR OTHER PLATFORMS」。
Step 2.
之後,按照你的作業系統下載對應的 SDK Tools,並且解壓縮到 eclipse 資料夾底下。
Step 3.
之後,打開好安裝完成的 Eclipse,並且按下【Help】→【Install New Software】。
Step 4.
接著,按下〔Add〕,並且在 Name 輸入 Android,在 Location 輸入「https://dl-ssl.google.com/android/eclipse/」。
Step 5.
之後,下載 Developer Tools,NDK 的話可自己選擇要不要下載。
Step 6.
之後,同意授權的內容後,按下【Finish】就下載完成囉!
Step 7.
下載完成後,如果是 Mac OS,請按下【eclipse】→【偏好設定】→【Android】來設定 Android SDK 的資料夾,如果是 Windows,則請按下【Windows】→【Prefernece】→【Android】來設定。
Step 8.
之後,按下【Windows】→【Android SDK Management】,並且安裝你要開發所對應的版本。
Step 9.
同樣的,下載前必須同意授權條約才可以安裝!
而在經歷了上述的動作後,相信你已經完成佈置 Android 的動作了,接下來就是開發囉!而如果你覺得這些實在太麻煩,那麼你有更簡單的選擇,詳情請見下面文章。

2014年10月24日 星期五

Gridview的分頁功能


https://social.msdn.microsoft.com/Forums/zh-TW/6453cf34-dba1-4ff1-be97-78a8a5712853/gridview?forum=236

先在 Page_Load
 protected void Page_Load(object sender, EventArgs e)
    {   
        if (!Page.IsPostBack)
        {
          // Ado.NET 的副程式
            BindGridView();
    
        }
    }
  protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNewsList.PageIndex =  e.NewPageIndex;
        BindGridView();
    }
    public void BindGridView()
    {       
        DataSet dsData = New DataSet() ; // 這邊就是你叫資料的給 DataTable or Dataset 的程式
         SqlDataAdapter sqlDA = new SqlDataAdapter(strCmd, sqlConn);
        sqlDA.Fill(dsData, "Data");
        gvNewsList.DataSource = dsData.Table["Data"];
        gvNewsList.DataBind();
    }


自己動手寫 GridView排序

2014年10月23日 星期四

GridView使用手寫連線


透過手寫程式了解GridView的運作
http://www.dotblogs.com.tw/topcat/archive/2009/02/23/7266.aspx


參考檔2

ADO.NET #3 (GridView + SqlDataSource)完全手寫、後置程式碼,兼論 SqlDataSource與UpdateParameter/DeleteParameter的用法

http://www.dotblogs.com.tw/mis2000lab/archive/2008/09/15/5377.aspx

參考資料3

【轉貼】ADO.NET #3 (GridView + SqlDataSource)完全手寫、

http://chery0805gamania.pixnet.net/blog/post/23993878-%E3%80%90%E8%BD%89%E8%B2%BC%E3%80%91ado.net-%233-(gridview-%2B-sqldatasource)%E5%AE%8C%E5%85%A8%E6%89%8B



簡單的搜尋引擎 + CheckBoxList(條件可複選)


[習題]簡單的搜尋引擎 + CheckBoxList(條件可複選)

http://www.dotblogs.com.tw/mis2000lab/archive/2008/11/18/searcheengine_checkboxlist_081118.aspx

修改後的 C# 程式如下:
01     protected void Button1_Click(object sender, EventArgs e)
02     {
03         string Search_String = "";
04         Boolean u_select = false;
05         int word_length = 0;
06 
07         for (int i = 0; i < CheckBoxList1.Items.Count; i++)
08         {
09             if (CheckBoxList1.Items[i].Selected)
10             {
11                 Search_String = Search_String + " [class] LIKE '%" + CheckBoxList1.Items[i].Text + "%' or ";
12                 u_select = true;   //使用者有點選任何一個CheckBoxList子選項 
13             }

14         }

15 
16         if (u_select)
17         {
18             word_length = Search_String.Length;  //計算 Search_String的字串長度,VB語法為Len(Search_String) 
19             Search_String = Left(Search_String, (word_length - 3));
20             //因為C#語法沒有 Left()函數,所以要自己寫!請看最下方。 
21             //刪去最後三個字 「or 」 
22           
23             Label1.Text = Search_String;
24         }

25         else
26         {
27             Label1.Text = "您尚未點選任何一個CheckBoxList子選項";
28             //Response.End();    //建議改成 return跳離。 
29             return;
30         }
      
31 
32         //======================================= 
33         //== SqlDataSource1 資料庫的連接字串 ConnectionString, 
34         //== 已事先寫在「HTML畫面的設定」裡面            == 
35         //======================================= 
36         SqlDataSource1.SelectCommand = "SELECT [test_time], [id], [class], [title] FROM [test] WHERE " + Search_String;
37         //這次不使用 SqlDataSource提供的@參數 
38     }

39 
40 
41     //因為C#語法沒有 Left()函數,所以要自己寫! 
42     public static string Left(string param, int length)
43     {
44         //we start at 0 since we want to get the characters starting from the 
45         //left and with the specified lenght and assign it to a variable 
46         string result = param.Substring(0, length);
47         //return the result of the operation 
48         return result;
49     }

 C# 補充說明:
1.  如果使用者未點選任何一個子選項,建議以  return跳開程式,並給予警告訊息。會比上述的 Response.End()來得好。
2. 下方的 Left()函數,因為是單一程式使用,可以免去 宣告時的 Static字眼。
3. 因為在HTML畫面,已經設定好GridView的 DataSourceID = "SqlDatasource1",所以不用寫 GridView.DataBind()。

VB版 程式如下:
01     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
02         Dim Search_String As String = ""
03         Dim u_select As Boolean = False
04         Dim i, word_length As Integer
05 
06         For i = 0 To (CheckBoxList1.Items.Count - 1)
07             If (CheckBoxList1.Items(i).Selected) Then
08                 Search_String = Search_String & " [class] LIKE '%" & CheckBoxList1.Items(i).Text & "%' or " 
09                 u_select = True    '--使用者有點選任何一個CheckBoxList子選項 
10             End If
11         Next
12 
13         If (u_select = TrueThen
14             word_length = Len(Search_String)    '--計算 Search_String的字串長度,VB語法為Len(Search_String) 
15             Search_String = Left(Search_String, (word_length - 3))
16             '--刪去最後三個字 「or 」 
17 
18             Label1.Text = Search_String
19         Else
20             Label1.Text = "您尚未點選任何一個CheckBoxList子選項"
21             '--Response.End()    '--改成 Exit Sub 跳開這個副程式 
22             Exit Sub
23         End If
24 
25         '======================================= 
26         '== SqlDataSource1 資料庫的連接字串 ConnectionString, 
27         '== 已事先寫在「HTML畫面的設定」裡面 == 
28         '======================================= 
29         SqlDataSource1.SelectCommand = "SELECT [test_time], [id], [class], [title] FROM [test] WHERE " & Search_String
30         '--這次不使用 SqlDataSource提供的@參數 
31     End Sub