ADO.net4 sqlAdapter+DataSet+Gridview 存取動作
1.Select
2.Update
JK test ok
'====================================
Imports System.Data.SqlClient
Imports System.Web.Configuration '.net4 可以會這樣把連線字串獨立出來
Partial Class Postpage
Inherits System.Web.UI.Page
Public ReadOnly Property pre_Control_Textbox1() As TextBox '公開這個 TextBox1 控制項給 後頁去存取
Get
Return TextBox1
End Get
End Property
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "KQ" '測試crossPost
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
sqlAction_Select() '依ID去查詢
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
sqlAction_update() '依ID去查詢
sqlAction_Select() '把查詢後的欄位資料分離取出後 再做更新
End Sub
Public Sub sqlAction_Select()
'依ID去查詢
'-----------
'可以先用sqldatasource元件自動產生連線字串 會放在web.config內
Dim conn_str = WebConfigurationManager.ConnectionStrings("db_webcamConnectionString").ConnectionString '使用第一組連線字串 (也可以用name 目前也只有放一組 )
Dim conn1 As New SqlConnection(conn_str)
Dim cmdstr = "SELECT Table_webcam.* FROM Table_webcam Where UserName=@UserName"
Dim sqlcmd = New SqlCommand(cmdstr, conn1)
sqlcmd.Parameters.AddWithValue("@UserName", TextBox1.Text)
Dim Adapter1 As SqlDataAdapter = New SqlDataAdapter(sqlcmd)
Dim dataset1 = New Data.DataSet() '產生一個名為test 的 DataSet
Adapter1.Fill(dataset1)
GridView2.DataSource = dataset1
If dataset1.Tables.Count > 0 Then '可以測試insert >> "的確有"回傳資料給 ds
GridView2.DataBind() '更新網頁容器GridView2
dataset1.Clear()
End If
'-----------取出每個row 的每個cell 的值----------------
Dim rows = GridView2.Rows.Count
Dim str_all As String = ""
For Each mm As GridViewRow In GridView2.Rows
Dim str_temp As String = ""
Dim cells_colum = mm.Cells().Count
For i = 0 To cells_colum - 1
str_temp = mm.Cells(i).Text.ToString.Trim
str_all = str_all + str_temp + "/"
Next
Next
Dim str_all_array = str_all.Split("/")
TextBox_area.Text = str_all_array(1)
TextBox_fps.Text = str_all_array(2)
TextBox_Meta.Text = str_all_array(3)
TextBox_email.Text = str_all_array(4)
'------------/取出每個row 的每個cell 的值/-----------------
End Sub
Public Sub sqlAction_update()
'把查詢後的欄位資料分離取出後 再做更新
'---------------------
'可以先用sqldatasource元件自動產生連線字串 會放在web.config內
Dim conn_str = WebConfigurationManager.ConnectionStrings("db_webcamConnectionString").ConnectionString '使用第一組連線字串 (也可以用name 目前也只有放一組 )
Dim conn1 As New SqlConnection(conn_str)
' Dim cmdstr_select = "SELECT Table_webcam.* FROM Table_webcam Where UserName=@UserName"
Dim cmdstr_update = "UPDATE Table_webcam SET area = @area, Email = @Email WHERE UserName = @UserName "
Dim sqlcmd = New SqlCommand(cmdstr_update, conn1)
sqlcmd.Parameters.AddWithValue("@UserName", TextBox1.Text)
sqlcmd.Parameters.AddWithValue("@area", TextBox_area.Text)
sqlcmd.Parameters.AddWithValue("@Email", TextBox_email.Text)
Dim Adapter1 As SqlDataAdapter = New SqlDataAdapter(sqlcmd)
Dim dataset1 = New Data.DataSet() '產生一個名為test 的 DataSet
Adapter1.Fill(dataset1)
GridView2.DataSource = dataset1
If dataset1.Tables.Count > 0 Then '可以測試insert >> "的確有"回傳資料給 ds
GridView2.DataBind() '更新網頁容器GridView2
dataset1.Clear()
End If
End Sub
End Class
留言列表