看板 Visual_Basic 關於我們 聯絡資訊
※ 引述《BigGreenFly (好大隻的綠色蒼蠅)》之銘言: : 請輸入專案類型(網站專案或者應用程式專案): : 網站專案 : 如題 : 我用的是SQL SERVER2005 : 希望有人可以解答 : 謝謝 給你一個範例 Imports System.Data.SqlClient Imports System.IO Imports System.Text Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim ConnectionString As String Dim cn As New SqlConnection() Dim cmd As New SqlCommand, dr As SqlDataReader = Nothing Try '連線至SQL Server ConnectionString = "Data Source=執行個體名稱;" & _ "Initial Catalog=資料庫名稱;" & _ "User ID=帳號;Password=密碼;" cn.ConnectionString = ConnectionString cn.Open() '取得資料 cmd.Connection = cn cmd.CommandText = "sql查詢語法" dr = cmd.ExecuteReader() '讀取並寫入資料至檔案 Dim sw As New StreamWriter("檔案路徑", True, Encoding.Default) Do While dr.Read() sw.WriteLine(dr("欄位名稱").ToString()) Loop sw.Close() Catch ex As Exception '錯誤處理 Label1.Text = ex.Message Finally '不管有沒有錯誤,結束時都要關閉連接 If Not dr Is Nothing AndAlso Not dr.IsClosed Then dr.Close() If Not cn Is Nothing AndAlso cn.State <> _ Data.ConnectionState.Closed Then cn.Close() End Try End Sub -- ▃▅▇▆▄ ▆▂▃ ` 逝去感如何能留住,半點遺留殊不易,██▅▇▄▃ ▇▃▂" . █████████▃i ▁▄▇ 更多悽悽慘慘的遭遇………██▆▃ █▅▆▃ˍ▄* ▂█▄▇▅▂. 我不知道,王~八~蛋~~! ▂▆███ █▄▃ 。fumizuki。Check。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.194.98.78 ※ 編輯: fumizuki 來自: 123.194.98.78 (09/13 13:06)
BigGreenFly:謝謝你唷 來去試試看^^ 09/13 23:14