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) & "')”


 

arrow
arrow
    文章標籤
    VB 資料庫
    全站熱搜

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