Dim dr as SqlDataReader
Dim Conn as SqlConnection
Dim cmd as SqlCommand
Conn= new SqlConnection("資料庫的連線字串,請自己修改")
cmd = new SqlCommand("SELECT * from 資料表", mySqlConnection)
try
Conn.Open()
dr = cmd.ExecuteReader()
do while (dr.Read())
'--把資料展現到畫面上。請自由發揮
loop
catch e as Exception
Response.Write(e.ToString())
finally
if Not (dr is Nothing) ' --關閉 DataReader
dr.Close()
end if
if (Conn.State = ConnectionState.Open) ' --關閉 DB的連線
Conn.Close()
end if
end try
//-----------------方法二-----------
Dim queryString As String = "SELECT * From 資料表;"
Using Conn As New SqlConnection("資料庫的連線字串")
Dim my_command As New SqlCommand(queryString, Conn)
Conn.Open()
'-- 資料庫連線!但後面「不」需要寫關閉的動作(Conn.Close()),因為Using....End Using 會自己處理
Dim reader As SqlDataReader = my_command.ExecuteReader()
While reader.Read()
'-- 自己寫程式,展現欄位的值。
End While
'-- 關閉DataReader
reader.Close()
End Using
沒有留言:
張貼留言