1. 程式人生 > >在asp.net中怎麼用VB連線SQL SERVER2005資料庫

在asp.net中怎麼用VB連線SQL SERVER2005資料庫

第一步:

Private Function ExecuteSQL(ByVal strSQL As String, ByRef errMsg As String) As DataSet 
        Dim cnn As SqlClient.SqlConnection 
        Dim cmd As New SqlClient.SqlCommand() 
        Dim adpt As SqlClient.SqlDataAdapter 
        Dim rst As New DataSet() 
        Dim SplitSQL() As String 
        errMsg = ""
Try SplitSQL = Split(strSQL) cnn = New SqlClient.SqlConnection("DataSource=192.168.0.1;Database=your_database;Uid=sa;Pwd=123123") If InStr("INSERT,DELETE,UPDATE", UCase$(SplitSQL(0))) Then cmd.Connection = cnn cmd.Connection.Open() cmd.CommandText = strSQL cmd.ExecuteNonQuery() Else
adpt = New SqlClient.SqlDataAdapter(strSQL, cnn) adpt.Fill(rst) ExecuteSQL = rst End If Catch ex As Exception errMsg = ex.Message Finally rst = Nothing cnn = Nothing End
Try End Function

第二步 呼叫:

GridView1.DataSource = ExecuteSQL("select * from tab'", "資料操作失敗!") 
GridView1.DataBind() 

說明:ExecuteSQL()兩個引數 1個是 SQL語句 另外一個是操作失敗時的提示資訊。直接在需要繫結資料的時候呼叫就是了。很簡單那 。並不需要你的積分 只希望能幫到你