顯示具有 VB.NET 技術技巧 標籤的文章。 顯示所有文章
顯示具有 VB.NET 技術技巧 標籤的文章。 顯示所有文章

2008年10月5日 星期日

DataSet與DataReader及資料庫連結運用(一)

關於在.NET裡的DataSet經過幾天的使用,稍微有了一些簡單的心得,DataSet裡包含有許多DataTable,這些DataTable彼此還可以透過DataRelation物件來建立表格的關聯性,加上有別於以往的Recordset必須持續連線的狀態,DataSet採用離線運作的模式,不會影響到Database的負擔。

而其中各種不同的元件關係如下圖。

基本上的觀念先釐清就方便了。




'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法一-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private dr As SqlDataReader
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    dr = cmd.ExecuteReader
    while dr.Read()
    txb_test.Text = dr("欄位名稱").ToString
    end while
    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法二-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand

    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    cmd.ExecuteNonQuery()

    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法三-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private dt As New DataTable
  Private dr As SqlDataReader
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    dr = cmd.ExecuteReader
    dt.Load(dr)
    obj_DataGridView.DataSource = dt
    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法四-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private da As New SqlDataAdapter
  Private ds As New DataSet
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    da.SelectCommand = cmd
    ds.Clear()
    da.Fill(ds, "自設表單名")
    dt.Load(dr)
    obj_DataGridView.DataSource = ds
    obj_DataGridView.DataMember = "自設表單名"

    進行各種ds運用    

    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法五-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private da As New SqlDataAdapter
  Private ds As New DataSet
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    da.SelectCommand = cmd
    ds.Clear()
    da.Fill(ds, "自設表單名")
    dt.Load(dr)
    obj_DataGridView.DataSource = ds
    obj_DataGridView.DataMember = "自設表單名"

    進行各種ds運用    

    cmd.Dispose()
    cnn.Close()


連結的方式多種,而取值也各有不同表示法

'-----------------------取值法一-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select top 流水號 count from hn_news order by counter desc"
  obj_txtBox.Text = cmd.ExecuteScalar.ToString

'單獨取得第一資料行的第一列值


'-----------------------取值法二-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select * from hn_news order by counter desc"
  dr = cmd.ExecuteReader
  dr.Read()   '讀一列
  obj_txtBox.Text = dr("欄位名稱").ToString

'單筆資料行的值,再依欄位名去抓取各欄位值


'-----------------------取值法三-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select * from hn_news order by counter desc"
  da.SelectCommand = cmd '使用SqlDataAdapter的SelectCommand
  ds.Clear()
  da.Fill(ds, "tab1") '將SqlDataAdapter
  obj_DataGridView.DataSource = ds
  obj_DataGridView.DataMember = "自設表單名"

  If MyDs.Tables(0).Columns.Count > 0 Then
    obj_txtBox.Text1 = ds.Tables(0).Columns(1).ColumnName
    obj_txtBox.Text2 = ds.Tables(0).Rows(0).Item("有效期限").ToString
    obj_txtBox.Text3 = ds.Tables(0).Rows(0).Item("驗收人代號").ToString
  End If



   關於取值法三中
    ds.Tables(0).Columns(1).ColumnName
 '表示取得table的欄位名稱,上圖中紅框區
    此處中Tables(0)可寫成Tables("表單名"),Columns(1)可寫成Columns("欄位名")

    
ds.Tables(0).Rows(0).Item("有效期限").ToString
    ds.Tables(0).Rows(0).Item("欄位名").ToString
    此處中Item("欄位名")可寫成Item(0) 'index
    如果寫ds.Tables(0).Rows(2).Item(4).ToString,表示是table(0)的第3行資料列 第5欄位的值

    
MyDs.Tables(0).Columns.Count
  '表示傳回的資料列數,用於判斷是否有傳回資料列
    
先暫存於此...

Read More...

2008年6月17日 星期二

DataGridView1.CurrentCellAddress紀錄

'----DataGridView在設計點選內容時,可用DataGridView1.CurrentCellAddress.X(或Y)來找到所選中的列或欄


Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick

  TextBox1.Text = DataGridView1.Item(0, DataGridView1.CurrentCellAddress.Y).Value
  TextBox2.Text = DataGridView1.Item(1, DataGridView1.CurrentCellAddress.Y).Value
  DataGridView1.SendToBack()

End Sub

Read More...

2008年5月29日 星期四

VB.net Directory與DirectoryInfo的運用_資料夾管理工具

由於自己有許多的音樂CD以及音樂檔案,透過一個個資料夾去進行整理與分類,為了個人習慣的問題,因此設計一個資料夾工具,主要提供資料夾名稱更改的功能,需求如下:

1.選取資料夾 (開啟A資料夾)
2.匯入選取的資料夾中所含子資料夾 (匯入A中的AA BB CC三個資料夾)
3.更改子資料夾內的所含子資料夾的名稱,使其與子資料夾相同 (將AA中的子資料夾改名為AA,將BB中的子資料夾改名為BB,將CC中的子資料夾改名為CC)

真遶口...算了 直接紀錄吧!!



Imports System.IO
Public Class 資料夾名稱管理
  Dim dir_Directory As DirectoryInfo '將最裡層的選擇資料夾 設為 DirectoryInfo物件 名為dir_Directory
  'd_str_tempFolderPath - 左邊list裡資料夾的路徑
  'd_str_tempFolderName - 左邊list裡選中的資料夾名稱
  Dim d_str_tempFolderPath, d_str_tempFolderName As String
  Private Sub obj_開啟_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles obj_開啟.Click
    obj_folderBD.ShowDialog()
  If obj_folderBD.SelectedPath.Count > 0 Then obj_listbox1.DataSource = Directory.GetDirectories(obj_folderBD.SelectedPath, "*.*")
    sub_歸零()
  End Sub 'obj_bt1_Click

  Private Sub obj_改名_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_改名.Click
    Do While Directory.Exists(d_str_tempFolderPath & "\" & d_str_tempFolderName)
      d_str_tempFolderName &= "I"
    Loop
    dir_Directory.MoveTo(d_str_tempFolderPath & "\" & d_str_tempFolderName)
    obj_文字.Text = "完成"
    sub_歸零()
  End Sub 'obj_改名_Click

  Private Sub obj_listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_listbox1.Click, obj_listbox1.SelectedIndexChanged
        sub_歸零()
    If obj_listbox1.Items.Count > 0 Then
      '將listBox2的來源設為 listbox1中選取的資料夾
      obj_ListBox2.DataSource = Directory.GetDirectories(obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString, "*.*")
      '給定d_str_tempFolderPath與d_str_tempFolderName的值
      d_str_tempFolderPath = obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString
      Dim dir_temp As New DirectoryInfo(obj_listbox1.Items(obj_listbox1.SelectedIndex.ToString).ToString)
      d_str_tempFolderName = dir_temp.Name
      obj_文字_資料夾.Text = dir_temp.Name
    End If
  End Sub 'obj_listbox1_Click

  Private Sub obj_ListBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_ListBox2.Click, obj_ListBox2.SelectedIndexChanged
    If obj_ListBox2.Items.Count > 0 Then
      obj_改名.Enabled = True
      '將最裡層的選擇資料夾 設為 DirectoryInfo物件 名為dir_Directory
      dir_Directory = New DirectoryInfo(obj_ListBox2.Items(obj_ListBox2.SelectedIndex.ToString).ToString)
      obj_文字_子資料夾.Text = dir_Directory.Name
    End If
  End Sub 'obj_ListBox2_Click

  Private Sub sub_歸零()
    d_str_tempFolderPath = Nothing
    d_str_tempFolderName = Nothing
    If d_str_tempFolderPath = Nothing Or d_str_tempFolderName = Nothing Then
      obj_改名.Enabled = False
    End If
  End Sub 'sub_歸零()


  Private Sub obj_批次改名_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles obj_批次改名.Click
    'dir_tempDir_1 表開啟之資料夾的類別
    'dir_tempDir_2 表開啟之資料夾中子資料夾的類別
    Dim dir_tempDir_1 As New DirectoryInfo(obj_folderBD.SelectedPath)
    Dim Ary_dir_1 As DirectoryInfo() = dir_tempDir_1.GetDirectories
    For i As Integer = 0 To Ary_dir_1.Length - 1
      Dim dir_tempDir_2 As New DirectoryInfo(Ary_dir_1(i).FullName)
      Dim Ary_dir_2 As DirectoryInfo() = dir_tempDir_2.GetDirectories
      '至此    Ary_dir_1 表左邊list的資料夾完整路徑 array
      '        Ary_dir_2 表右邊list的資料夾完整路徑 array
      d_str_tempFolderPath = dir_tempDir_2.FullName
      d_str_tempFolderName = dir_tempDir_2.Name
      If Ary_dir_2.Length > 0 Then
        For Each tmp In Ary_dir_2
          Do While Directory.Exists(d_str_tempFolderPath & "\" & d_str_tempFolderName)
            d_str_tempFolderName &= "I"
          Loop
        tmp.MoveTo(d_str_tempFolderPath & "\" & d_str_tempFolderName)
        Next
      End If
    Next
    obj_文字.Text = "完成批次更名"
  End Sub
End Class


'obj_listbox1.Items(0).ToString 得到listbox中第一個選項目
'obj_listbox1.SelectedIndex.ToString 得到listbox被選取的項目的index


'--------宣告法一
'    Dim dir_temp As DirectoryInfo
'    dir_temp = New DirectoryInfo("C:\Documents and Settings\g_user\桌面\誠工具")
'--------宣告法二
'    Dim dir_temp2 As New DirectoryInfo("C:\Documents and Settings\g_user\桌面\測試dir")
'
'--------宣告並同時建立子資料夾
'    Dim dir_temp2 As New DirectoryInfo("C:\Documents and Settings\g_user\桌面\測試dir")
'    Dim dir_sub1 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir1")
'    Dim dir_sub2 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir2")
'    Dim dir_sub3 As DirectoryInfo = dir_temp2.CreateSubdirectory("subDir3")

Read More...

2008年5月26日 星期一

VB.net_將資料填進EXCEl與WORD中

整理一下最近對於如何將資料填進已經設好檔案的word與excel中的紀錄。

關於使用VB.NET將資料匯進WORD與EXCEL,主要都是透過OFFICE的參考,運用各自的類別,一層一層建立它們的各自的屬性,像是WORD從外到內的Application、Document、Table以及EXCEL從外到內的Application、Workbook、Worksheet,在連接上彼此並運用各自的Method來進行工作。首先先來看word的程式碼


Imports Microsoft.Office.Interop

Dim wod_app As New Word.Application    '這邊要注意建立app時要用new
Dim wod_doc As Word.Document
Dim wod_tab As Word.Table

  wod_doc = wod_app.Documents.Open("C:\NEWS管理.doc", , True)
  wod_tab = wod_doc.Tables.Item(1)

  contSqlconn(1)    '個人的連接資料庫函式
  cmd.CommandText = "select * from hn_news where (流水號='" + 流水號.Text + "')"
  dr = cmd.ExecuteReader
  dr.Read()

    wod_tab.Cell(1, 1).Range.Text = dr("狀態旗標").ToString
    wod_tab.Cell(1, 2).Range.Text = dr("流水號").ToString
    wod_tab.Cell(1, 4).Range.Text = dr("標題").ToString
    wod_tab.Cell(2, 2).Range.Text = dr("內容").ToString
    wod_tab.Cell(4, 2).Range.Text = dr("回覆").ToString

  dr.Close()
  contSqlconn(-1)

  wod_app.Visible = True '預覽列印
  'wod_app.PrintOut(Copies:=1) '直接列印
  'wod_app.Quit(SaveChanges:=False) '關閉開啟的WORD

  wod_tab = Nothing

  wod_doc.Close()
  wod_doc = Nothing

  wod_app.Quit()
  wod_app = Nothing
  
  GC.Collect()


EXCEL:

Imports Microsoft.Office.Interop

  Dim exl_ap As New Excel.Application
  Dim exl_wkb As Excel.Workbook
  Dim exl_wks As Excel.Worksheet
  Dim i As Integer = 1

  exl_wkb = exl_app.Workbooks.Open("C:\驗購表.xls", , True) '這個TRUE是Readonly
  exl_wks = exl_wkb.Worksheets("sheet1") '或者 exl_wks = exl_wkb.Worksheets(1)

  contSqlconn(1)
  cmd.CommandText = "sql語法省略" + _
  dr = cmd.ExecuteReader
    With exl_wks
      .Name = "藥品驗購表"
      .Range("A" & i).Value = "藥品中文名"
      .Range("B" & i).Value = "藥品索引"
      .Range("C" & i).Value = "藥品簡稱"
      .Range("D" & i).Value = "驗收日期"
      .Range("E" & i).Value = "藥廠名稱"
      .Range("F" & i).Value = "藥廠代號"
      .Range("A1:F1").Interior.ColorIndex = 6
      .Cells().ColumnWidth = 7

    End With
  While dr.Read
    i += 1
    With exl_wks
      .Range("A" & i).Value = dr(0).ToString
      .Range("B" & i).Value = dr(1).ToString
      .Range("C" & i).Value = dr(2).ToString
      .Range("D" & i).Value = dr(3).ToString
      .Range("E" & i).Value = dr(4).ToString
      .Range("F" & i).Value = dr(5).ToString
    End With
  End While

  exl_app.SaveWorkspace()
  exl_app.Visible = True

  exl_wks = Nothing

  exl_wkb.Close()
  exl_wkb = Nothing

  exl_app.Quit()
  exl_app = Nothing

  contSqlconn(-1)
  GC.Collect()


大至上就是如此,特別的地方再說一下。

這個word是用填表的方式所以才會有table,也就是先在預設好的word裡畫好table表格,然後算他的欄位去填入資料,但也有另一種方式不用畫表格的,就是運用word裡的書籤功能,至於這個等我有例子在作紀錄。

不管是word還是excel,都必須要在使用前先加入各自的參考(dll),這點可是不能忘的。

Read More...

2008年5月15日 星期四

VB.NET_DataSet與DataReader及其相關資料庫連結運用(一)

關於在.NET裡的DataSet經過幾天的使用,稍微有了一些簡單的心得,DataSet裡包含有許多DataTable,這些DataTable彼此還可以透過DataRelation物件來建立表格的關聯性,加上有別於以往的Recordset必須持續連線的狀態,DataSet採用離線運作的模式,不會影響到Database的負擔。


而其中各種不同的元件關係如下圖。



基本上的觀念先釐清就方便了。


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法一-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private dr As SqlDataReader
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    dr = cmd.ExecuteReader
    while dr.Read()
    txb_test.Text = dr("欄位名稱").ToString
    end while
    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法二-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand

    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    cmd.ExecuteNonQuery()

    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法三-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private dt As New DataTable
  Private dr As SqlDataReader
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    dr = cmd.ExecuteReader
    dt.Load(dr)
    obj_DataGridView.DataSource = dt
    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法四-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private da As New SqlDataAdapter
  Private ds As New DataSet
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    da.SelectCommand = cmd
    ds.Clear()
    da.Fill(ds, "自設表單名")
    dt.Load(dr)
    obj_DataGridView.DataSource = ds
    obj_DataGridView.DataMember = "自設表單名"

    進行各種ds運用    

    cmd.Dispose()
    cnn.Close()


'--------------------------------------分格線---------------------------------------'
'--------------------------------------連結法五-------------------------------------'
Imports System.Data.SqlClient
  Private cnn As New SqlConnection
  Private cmd As New SqlCommand
  Private da As New SqlDataAdapter
  Private ds As New DataSet
    cnn.ConnectionString = "Data Source=G_HISBKS;Initial Catalog=XXX;User ID=XXX"
    cnn.Open()

    cmd.Connection = cnn
    cmd.CommandText = "下SQL語法"
    da.SelectCommand = cmd
    ds.Clear()
    da.Fill(ds, "自設表單名")
    dt.Load(dr)
    obj_DataGridView.DataSource = ds
    obj_DataGridView.DataMember = "自設表單名"

    進行各種ds運用    

    cmd.Dispose()
    cnn.Close()


連結的方式多種,而取值也各有不同表示法

'-----------------------取值法一-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select top 流水號 count from hn_news order by counter desc"
  obj_txtBox.Text = cmd.ExecuteScalar.ToString

'單獨取得第一資料行的第一列值


'-----------------------取值法二-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select * from hn_news order by counter desc"
  dr = cmd.ExecuteReader
  dr.Read()   '讀一列
  obj_txtBox.Text = dr("欄位名稱").ToString

'單筆資料行的值,再依欄位名去抓取各欄位值


'-----------------------取值法三-----------------------------------------------------
'------------------------------------------------------------------------------------
  cmd.CommandText = "select * from hn_news order by counter desc"
  da.SelectCommand = cmd '使用SqlDataAdapter的SelectCommand
  ds.Clear()
  da.Fill(ds, "tab1") '將SqlDataAdapter
  obj_DataGridView.DataSource = ds
  obj_DataGridView.DataMember = "自設表單名"

  If MyDs.Tables(0).Columns.Count > 0 Then
    obj_txtBox.Text1 = ds.Tables(0).Columns(1).ColumnName
    obj_txtBox.Text2 = ds.Tables(0).Rows(0).Item("有效期限").ToString
    obj_txtBox.Text3 = ds.Tables(0).Rows(0).Item("驗收人代號").ToString
  End If


    關於取值法三中
    ds.Tables(0).Columns(1).ColumnName '表示取得table的欄位名稱,上圖中紅框區
    此處中Tables(0)可寫成Tables("表單名"),Columns(1)可寫成Columns("欄位名")

    ds.Tables(0).Rows(0).Item("有效期限").ToString
    ds.Tables(0).Rows(0).Item("欄位名").ToString
    此處中Item("欄位名")可寫成Item(0) 'index
    如果寫ds.Tables(0).Rows(2).Item(4).ToString,表示是table(0)的第3行資料列 第5欄位的值

    MyDs.Tables(0).Columns.Count  '表示傳回的資料列數,用於判斷是否有傳回資料列
    
先暫存於此...

程式碼欄位

Read More...

2008年5月11日 星期日

在vb.net裡用vb的函式

這應該是屬於我的愚蠢,不過還是紀錄一下,像是.net裡已經沒有原本的right()或left(),但是如果依然想使用的話,可以這樣寫


Microsoft.VisualBasic.函式
例如:

Microsoft.VisualBasic.Right(s,2)
Microsoft.VisualBasic.Left(s,2)

Read More...

2008年5月8日 星期四

.NET中 tag的一個小應用

tag的一個小應用



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As Control

  For Each con In Me.Controls
    If TypeOf (con) Is TextBox Then
      Dim txt As TextBox = con
      If txt.Tag = "a" Then
        txt.Text = ""
      End If
    End If
  Next

End Sub

Read More...

2008年4月30日 星期三

動態增加控制元件


Dim mylb As TextBox = New TextBox
Me.Controls.Add(mylb)
mylb.Text = "Hello,VB.NET"

Read More...

VB.NET中寫入文件的同時,並覆蓋原文件內容


Dim strFilePath As String = "F:\\1.txt"
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(strFilePath, False)
Dim temp As String = "1111111111111111111111111"
sw.WriteLine(temp)
sw.Flush()
sw.Close()
sw = Nothing


如果 Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(strFilePath, False)
改成 Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(strFilePath, true)
就是往後追加的

Read More...