ASP.Net/ADO.net Database/Update SqlConnection

Материал из .Net Framework эксперт
Версия от 11:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Update Sql Server Data

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim DBConn as SQLConnection
    Dim DBUpdate As New SQLCommand
"        DBConn = New SQLConnection("server=localhost;" _
 "           & "Initial Catalog=TT;" _
  "          & "User Id=sa;" _
   "         & "Password=yourpassword;")
    DBConn = New SQLConnection("Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";")
    DBUpdate.rumandText = "Update Employee Set " _
        & "Salary = Salary * " & txtRaise.Text
    DBUpdate.Connection = DBConn
    DBUpdate.Connection.Open
    DBUpdate.ExecuteNonQuery()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Updating SQL Server Data</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
Enter the raise for all employees. For example, enter 1.25 for a 25% raise:
<BR>
<asp:textbox
    id="txtRaise"
    runat="Server"
/>
<BR><BR>
<asp:button 
    id="butOK"
    text="  OK  "
    onclick="SubmitBtn_Click" 
    runat="server"
/>  
</form>
</BODY>
</HTML>



Updating a database

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
  <head>
    <title>Updating a database</title>
  </head>
  <body>
    <form method="post" runat="server">
      <asp:Label id="txtFirstName" runat="server" /><br/><br/>
      <asp:TextBox id="txtLastName" runat="server" /><br/><br/>
      <asp:Button id="btnChange" runat="server"
                  OnClick="btnChange_Click" Text="Change" />
    </form>
  </body>
</html>
<script language="VB" runat="server">
Dim strConnection As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";"
Dim objConnection As SqlConnection
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
  If Not IsPostBack Then
    LoadProduct()
  End If
End Sub
Sub LoadProduct()
  Connect()
  Dim strQuery As String = "SELECT FirstName, LastName " & _
                           "FROM Employee"
  Dim dbComm As New SqlCommand(strQuery, objConnection)
  Dim reader As SqlDataReader = dbComm.ExecuteReader()
  reader.Read()
  txtFirstName.Text = reader.GetString(0)
  txtLastName.Text = reader.GetString(0)
  reader.Close()
  Disconnect()
End Sub
Sub btnChange_Click(ByVal Sender As Object, ByVal E As EventArgs)
  UpdateProduct()
  LoadProduct()
End Sub
Private Sub UpdateProduct()
  Dim strSQL As String = "UPDATE Employee" & _
                         " SET FirstName = " & txtLastName.Text & _
                         " WHERE ID = 1"
  Connect()
  Dim dbComm As New SqlCommand(strSQL, objConnection)
  dbComm.ExecuteNonQuery()
  Disconnect()
End Sub
Private Sub Connect()
  If objConnection Is Nothing Then
    objConnection = New SqlConnection(strConnection)
  End If
  If objConnection.State = ConnectionState.Closed Then
    objConnection.Open()
  End If
End Sub
Private Sub Disconnect()
  objConnection.Close()
End Sub
</script>