I am an old classic ASP (potsey) wanting to be a cool asp.net (fonz). However, after this ordeal I'm almost ready to be satisified with being good ole "un-cool".
I just want to throw a simple UPDATE statement to the backend. So the uncool Classic ASP way is as follows:
<!-- METADATA TYPE="typelib" FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%Option Explicit%>
<%
Dim myConnection鎱章Connection object
Dim myConnectionStr鎱章Connection String
Dim myCommand鎱章Command object
Dim myCommandSQL鎱章SQL String
Set myConnection = Server.CreateObject("ADODB.Connection")
myConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataStores\myDb\myDb.mdb;"
myConnection.Open myConnectionStr
Set myCommand = Server.CreateObject("ADODB.Command")
myCommandSQL = "UPDATE myTable SET myField = 鎲亂Value?WHERE (myPK = 鎲亂PKValue?;"
With myCommand
.ActiveConnection = myConnection
.CommandText = myCommandSQL
.CommandType = adCmdText
.Execute
End With
Set myCommand = Nothing
myConnection.Close
Set myConnection = Nothing
Response.Redirect("someOtherASP.asp")
%>
and now my cool version:
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Namespace myNS
Public Class myCodeBehind : Inherits Page
sub Page_Load(s As Object, e As EventArgs)
Dim myConnection As OleDbConnection鎱章Connection object
Dim myConnectionStr As String鎱章Connection String
Dim mySelectCommand As OleDbCommand鎱章Select Command to populate dataSet
Dim mySelectCommandSQL As String鎱章Select SQL String
Dim myAdapter As OleDbDataAdapter = New OleDbDataAdapter()鎱硈p.net dataAdapter object
Dim myDataSet As New DataSet()鎱硈p.net dataSet object
Dim myDataTable As DataTable鎱硈p.net dataTable object
Dim myRowsToUpdate() As DataRow鎱硈p.net array of dataRow objects
Dim myCurrentRow As DataRow鎱硈p.net dataRow object
Dim myCommandBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(oAdapter)鎱硈p.net idontknowwhatthecrap
'build the connection to the db
myConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataStores\myDb\myDb.mdb;"
myConnection = New OleDbConnection(myConnectionStr)
'build the command object to issue to the dataAdapter
mySelectCommandSQL = "SELECT * FROM myTable WHERE (myPK = 鎲亂PKValue?;"
mySelectCommand = New OleDbCommand(mySelectCommandSQL, myConnection)
'set the dataAdapter's selectCommand and Connection properties
myAdapter.SelectCommand = mySelectCommand
'populate the dataSet
myAdapter.Fill(myDataSet, "myTable")
'get a handle on the dataSet's Table object
myDataTable = myDataSet.Tables("myTable")
'get a handle on the Table's records
myRowsToUpdate = myDataTable.Select()
'loop through the records and modify column values
'this being one record due to WHERE clause
For Each myCurrentRow In myRowsToUpdate
myCurrentRow.Item("myField ") = 鎼å¹Value?br />Next
'Im guessing this modifies the dataAdapter's updateCommand property
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand()
'and finally issue the update back to the database
myAdapter.Update(myDataSet, "myTable")
End Sub
End Class
End Namespace
can anyone please tell me I've gone the long way around here and that there is a much simpler way. I just want to send an SQL command to the database but the .net version not only isn't very cool its downright ugly. Thanks much.You've gone the very long way around. All you need to do is issue the UPDATE command; you don't need to pull anything from the database.
EDIT: you only need a OleDbConnection and a OleDbCommand, none of that other stuff.Like CardboardHammer said, you just need the update command. Similar to this.
Dim myConn as New SqlConnection(--Your connection string here--)
dim myCmd as New SqlCommand(UPDATE myTable SET myField = @myValue WHERE myPK = @myPKValue",myConn)
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@myValue", myValue)
myCmd.Parameters.Add("@myPKValue", myPKValue)
myConn.Open()
myCmd.ExecuteNonQuery()
myConn.Close()
'cleanup
myCmd.dispose()
myConn.dispose()
I just want to throw a simple UPDATE statement to the backend. So the uncool Classic ASP way is as follows:
<!-- METADATA TYPE="typelib" FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%Option Explicit%>
<%
Dim myConnection鎱章Connection object
Dim myConnectionStr鎱章Connection String
Dim myCommand鎱章Command object
Dim myCommandSQL鎱章SQL String
Set myConnection = Server.CreateObject("ADODB.Connection")
myConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataStores\myDb\myDb.mdb;"
myConnection.Open myConnectionStr
Set myCommand = Server.CreateObject("ADODB.Command")
myCommandSQL = "UPDATE myTable SET myField = 鎲亂Value?WHERE (myPK = 鎲亂PKValue?;"
With myCommand
.ActiveConnection = myConnection
.CommandText = myCommandSQL
.CommandType = adCmdText
.Execute
End With
Set myCommand = Nothing
myConnection.Close
Set myConnection = Nothing
Response.Redirect("someOtherASP.asp")
%>
and now my cool version:
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Namespace myNS
Public Class myCodeBehind : Inherits Page
sub Page_Load(s As Object, e As EventArgs)
Dim myConnection As OleDbConnection鎱章Connection object
Dim myConnectionStr As String鎱章Connection String
Dim mySelectCommand As OleDbCommand鎱章Select Command to populate dataSet
Dim mySelectCommandSQL As String鎱章Select SQL String
Dim myAdapter As OleDbDataAdapter = New OleDbDataAdapter()鎱硈p.net dataAdapter object
Dim myDataSet As New DataSet()鎱硈p.net dataSet object
Dim myDataTable As DataTable鎱硈p.net dataTable object
Dim myRowsToUpdate() As DataRow鎱硈p.net array of dataRow objects
Dim myCurrentRow As DataRow鎱硈p.net dataRow object
Dim myCommandBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(oAdapter)鎱硈p.net idontknowwhatthecrap
'build the connection to the db
myConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataStores\myDb\myDb.mdb;"
myConnection = New OleDbConnection(myConnectionStr)
'build the command object to issue to the dataAdapter
mySelectCommandSQL = "SELECT * FROM myTable WHERE (myPK = 鎲亂PKValue?;"
mySelectCommand = New OleDbCommand(mySelectCommandSQL, myConnection)
'set the dataAdapter's selectCommand and Connection properties
myAdapter.SelectCommand = mySelectCommand
'populate the dataSet
myAdapter.Fill(myDataSet, "myTable")
'get a handle on the dataSet's Table object
myDataTable = myDataSet.Tables("myTable")
'get a handle on the Table's records
myRowsToUpdate = myDataTable.Select()
'loop through the records and modify column values
'this being one record due to WHERE clause
For Each myCurrentRow In myRowsToUpdate
myCurrentRow.Item("myField ") = 鎼å¹Value?br />Next
'Im guessing this modifies the dataAdapter's updateCommand property
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand()
'and finally issue the update back to the database
myAdapter.Update(myDataSet, "myTable")
End Sub
End Class
End Namespace
can anyone please tell me I've gone the long way around here and that there is a much simpler way. I just want to send an SQL command to the database but the .net version not only isn't very cool its downright ugly. Thanks much.You've gone the very long way around. All you need to do is issue the UPDATE command; you don't need to pull anything from the database.
EDIT: you only need a OleDbConnection and a OleDbCommand, none of that other stuff.Like CardboardHammer said, you just need the update command. Similar to this.
Dim myConn as New SqlConnection(--Your connection string here--)
dim myCmd as New SqlCommand(UPDATE myTable SET myField = @myValue WHERE myPK = @myPKValue",myConn)
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@myValue", myValue)
myCmd.Parameters.Add("@myPKValue", myPKValue)
myConn.Open()
myCmd.ExecuteNonQuery()
myConn.Close()
'cleanup
myCmd.dispose()
myConn.dispose()