連結資料庫四大步驟(以 ASP.NET 為例)
http://findbook.tw/book/9789862042380/basic
http://findbook.tw/book/9789862042380/basic
'第一:連結SQL資料庫
Dim connstr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\test_Data.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim conn As New SqlConnection(connstr)
conn.Open()
'第二:執行SQL指令
Dim sqlstr As String = "select * from test"
Dim cmd As New SqlCommand(sqlstr, conn)
Dim sqlstr As String = "select * from test"
Dim cmd As New SqlCommand(sqlstr, conn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
'第三:印出資料
While dr.Read()
Response.Write("文章編號:" + dr("id") & "<br>")
Response.Write("日 期:" + dr("test_time") & "<br>")
Response.Write("文章標題:" + dr("title"))
Response.Write("<hr>")
End While
While dr.Read()
Response.Write("文章編號:" + dr("id") & "<br>")
Response.Write("日 期:" + dr("test_time") & "<br>")
Response.Write("文章標題:" + dr("title"))
Response.Write("<hr>")
End While
'第四:關閉連線
cmd.Cancel()
dr.Close()
conn.Close()
conn.Dispose()
cmd.Cancel()
dr.Close()
conn.Close()
conn.Dispose()
沒有留言:
張貼留言