⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 admin_update_database.aspx

📁 用C#,VB和SQLSERVER编写的远程教学程序
💻 ASPX
字号:
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<HTML>
	<script language="VB" runat="server">

    Dim MyConnection As SqlConnection

    Sub Page_Load(Sender As Object, E As EventArgs)

        MyConnection = New _
            SqlConnection("workstation id=SONGWEI;packet size=4096;user id=sa;password=sql;data source=SONGWEI;persist se" & _
        "curity info=False;initial catalog=vodweb")
         if pagemode2.checked then
      MyDataGrid.pagerstyle.mode=pagermode.numericpages
     else
     MyDataGrid.pagerstyle.mode=pagermode.nextprev
     MyDataGrid.pagerstyle.prevpagetext="上一页"
     MyDataGrid.pagerstyle.nextpagetext="下一页"
     end if
        If Not (IsPostBack)
             BindGrid("userid")
        End If
    End Sub

    Sub MyDataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
        MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
         BindGrid("userid")
    End Sub

    Sub MyDataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)
        MyDataGrid.EditItemIndex = -1
         BindGrid("userid")
    End Sub

    Sub MyDataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
        Dim DS As DataSet
        Dim MyCommand As SqlCommand
       Dim UpdateCmd As String = "UPDATE reg SET userid = @Userid,password=@Password,Email=@Email,addr=@Addr,phone=Phone,sex=@Sex,birth=@Birth where userid=@Userid" 
     MyCommand = New SqlCommand(UpdateCmd, MyConnection)
       MyCommand.Parameters.Add(New SqlParameter("@Userid", SqlDbType.NChar, 10))
        MyCommand.Parameters.Add(New SqlParameter("@Password", SqlDbType.NChar, 10))
        MyCommand.Parameters.Add(New SqlParameter("@Email", SqlDbType.NVarChar, 50)) 
        MyCommand.Parameters.Add(New SqlParameter("@Addr",  SqlDbType.NVarChar, 50)) 
        MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NVarChar, 20))  
        MyCommand.Parameters.Add(New SqlParameter("@Sex", SqlDbType.NChar, 10))    
        MyCommand.Parameters.Add(New SqlParameter("@Birth", SqlDbType.NChar, 10))    
        MyCommand.Parameters("@Userid").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
       Dim Cols As String() = {"@Userid","@Password","@Email","@Addr","@Phone","@Sex","@Birth"}
         Dim NumCols As Integer = E.Item.Cells.Count
        Message.InnerHtml = ""
        Dim I As Integer
        For I=2 To NumCols-1 'skip first, second and last column
            Dim CurrentTextBox As System.Web.UI.WebControls.TextBox
            CurrentTextBox = E.Item.Cells(I).Controls(0)
            Dim ColValue As String = CurrentTextBox.Text


            ' check for invalid values           
            ' Check for null values in required fields
            If  ColValue = ""
                Message.InnerHtml &= Cols(i-1) & "出错:  不允许为空值 "  & "<br>"
            End If
            MyCommand.Parameters(Cols(I-1)).Value = ColValue
        Next
        If Message.InnerHtml <> ""
            Message.Style("color") = "red"
            Return
        End If
        MyCommand.Connection.Open()
        Try
            MyCommand.ExecuteNonQuery()
            Message.InnerHtml = "<b>更新记录成功</b><br>"
            MyDataGrid.EditItemIndex = -1
        Catch Exp As SQLException
            If Exp.Number = 2627
                Message.InnerHtml = "ERROR: A record already exists with the same " & _
                    "primary key"
            Else
                Message.InnerHtml = "ERROR: Could not update record, please " & _
                    "ensure the fields are correctly filled out"
            End If
            Message.Style("color") = "red"
        End Try

        MyCommand.Connection.Close()
         BindGrid("userid")
    End Sub
    
    
   
    Sub BindGrid(SortField as string)

        Dim DS As DataSet
        Dim MyCommand As SqlDataAdapter     
        MyCommand = new SqlDataAdapter("select * from reg", MyConnection)
        DS = new DataSet()
        MyCommand.Fill(DS, "用户信息")
        dim source as  dataview=DS.Tables("用户信息").DefaultView
        MyDataGrid.DataSource=source
        source.sort=SortField
        MyDataGrid.DataBind()
    End Sub

    sub MyDataGrid_PageIndexChanged(sender as object ,e as datagridpagechangedeventargs)
    MyDataGrid.currentpageindex=e.newpageindex
     BindGrid("userid")
    end sub
    
    Sub MyDataGrid_Sort(Src As Object, E As DataGridSortCommandEventArgs)
    BindGrid(E.SortExpression)
    End Sub
	</script>
	<body style="FONT: 10pt verdana">
		<form runat="server" ID="Form1">
			<h3><font face="Verdana">管理员编缉数据库</font></h3>
			<span id="Message" EnableViewState="false" runat="server" /><p>
				<ASP:DataGrid id="MyDataGrid" runat="server" Width="800" BackColor="#ccccff" BorderColor="black"
					ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
					OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel" OnUpdateCommand="MyDataGrid_Update"
					DataKeyField="userid" OnPageIndexChanged="MyDataGrid_PageIndexChanged" OnSortCommand="MyDataGrid_Sort"
					AllowPaging="True" AllowSorting="True">
					<Columns>
						<asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false" />
					</Columns>
				</ASP:DataGrid>
				<center>
					页码模式:
					<asp:RadioButton Runat="server" ID="pagemode2" AutoPostBack="True" Checked="True" GroupName="页码模式"
						Text="上下页模式" />
					<asp:RadioButton Runat="server" ID="pagemode1" AutoPostBack="True" Checked="True" GroupName="页码模式"
						Text="数字模式" />
				</center>
		</form>
	</body>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -