📄 updateuser.aspx.vb
字号:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class updateuser
Inherits System.Web.UI.Page
Protected WithEvents Message As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Dim myConnection As SqlConnection
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
myConnection = New SqlConnection("user id=sa;password=;server=qianzh;" _
& "database=library")
If Not IsPostBack Then
BindGrid()
End If
End Sub
'建立一个索引,然后绑定数据库
Sub MyDataGrid_Edit(ByVal sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
'如果cancel,则重新原来的数据库绑定
Sub MyDataGrid_Cancel(ByVal sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
'更新数据库
Public Sub MyDataGrid_Update(ByVal sender As Object, _
ByVal E As DataGridCommandEventArgs)
Dim updateCmd As String = "UPDATE reader SET userid = @Id," _
& " password = @password, uname = @uname, sex = @sex," _
& " dep = @dep WHERE userid = @Id;"
Dim myCommand As SqlCommand = New SqlCommand(updateCmd, _
myConnection)
myCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.VarChar))
myCommand.Parameters.Add(New SqlParameter("@password", _
SqlDbType.VarChar))
myCommand.Parameters.Add(New SqlParameter("@uname", _
SqlDbType.VarChar))
myCommand.Parameters.Add(New SqlParameter("@sex", _
SqlDbType.VarChar))
myCommand.Parameters.Add(New SqlParameter("@dep", _
SqlDbType.VarChar))
myCommand.Parameters("@Id").Value = _
MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Dim cols() As String = {"@Id", "@password", "@uname", "@sex", _
"@dep"}
'检查是否有空的数据
Dim numCols As Integer = E.Item.Cells.Count
Dim i As Integer
Dim colvalue As String
Dim txtBox As TextBox
For i = 2 To numCols - 1
txtBox = E.Item.Cells(i).Controls(0)
colvalue = txtBox.Text
If (i < 4 And colvalue = "") Then
Message.InnerHtml = "数据段不允许为空"
Message.Style("color") = "red"
Exit Sub
End If
myCommand.Parameters(cols(i - 1)).Value = colvalue
Next i
txtBox = E.Item.Cells(numCols - 1).Controls(0)
' 连接数据库,更新数据
myCommand.Connection.Open()
Try
myCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>数据已被更新</b><br>"
MyDataGrid.EditItemIndex = -1
Catch ex As SqlException
If ex.Number = 2627 Then
Message.InnerHtml = "该纪录已经存在"
Else
Message.InnerHtml = "不能更新,请确认书如是否正确."
Message.Style("color") = "red"
End If
End Try
'关闭连接,重新绑定数据库
myCommand.Connection.Close()
BindGrid()
' End If
End Sub
'绑定数据库子程
Public Sub BindGrid()
Dim myConnection As SqlConnection = _
New SqlConnection("user id=sa;password=;server=qianzh;" _
& "database=library")
Dim myCommand As SqlDataAdapter = New SqlDataAdapter("SELECT *" _
& " FROM reader", myConnection)
Dim ds As DataSet = New DataSet()
myCommand.Fill(ds)
MyDataGrid.DataSource = ds
MyDataGrid.DataBind()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -