作者fumizuki (蒙面加菲狮)
看板Visual_Basic
标题Re: [.NET] 请问如何用VB语法将SQL理想要的资料写ꐠ…
时间Sat Sep 13 13:04:02 2008
※ 引述《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)
1F:→ BigGreenFly:谢谢你唷 来去试试看^^ 09/13 23:14