I'm having a hard time understanding why I can't get this transaction to roll back. I have a DataAccess object that handles the connection/transaction. I have a MailingData business object, and a MailingDataAccess object which takes the DataAccess object byRef from the MailingData object and executes a stored procedure. I immediately rollback the update, but it doesn't roll back. I'm guessing this is a scope issue. Help? I was doing byval with dataaccess, but that wasn't working so I changed to byref.
vb.net console app -
code:
Dim da As DataAccess = New DataAccess("mydb")
Try
' business object
Dim mailingData as New MailingData()
' start a transaction
da.StartTransaction()
' do stuff, set properties...
' pass the data access object byref to a method that calls a stored proc
mailingData.UpdateCategorize(da)
' testing, this doesn't roll back the update from the above stored proc
da.RollBackTransaction()
'da.CommitTransaction()
Catch ex As Exception
da.RollBackTransaction()
End Try
My data access object:
Private _database As Database
Private _transaction As Data.IDbTransaction
Private _connection As Data.IDbConnection
Public ReadOnly Property Database() As Database
Get
Return _database
End Get
End Property
Public ReadOnly Property Connection() As IDbConnection
Get
Return _connection
End Get
End Property
Public ReadOnly Property Transaction() As Data.IDbTransaction
Get
Return _transaction
End Get
End Property
Public Sub New()
_database = GetDatabase()
End Sub
Public Sub New(ByVal database As String)
_database = GetDatabase(database)
End Sub
Private Function GetDatabase() As Database
Return DatabaseFactory.CreateDatabase()
End Function
Private Function GetDatabase(ByVal database As String) As Database
Return DatabaseFactory.CreateDatabase(database)
End Function
'-- if not transaction started, start new one.
Public Sub StartTransaction()
If _transaction Is Nothing Then
_connection = _database.CreateConnection()
_connection.Open()
_transaction = _connection.BeginTransaction
End If
End Sub
Public Sub CommitTransaction()
_transaction.Commit()
If Not IsNothing(Connection) Then
Connection.Close()
End If
End Sub
Public Sub RollBackTransaction()
_transaction.Rollback()
If Not IsNothing(Connection) Then
Connection.Close()
End If
End Sub
End Class
mailingData.UpdateCategorize() is defined within the mailingData business object as:
Public Function UpdateCategorize(ByRef da As DataAccess) As Integer
Dim mailingDA As New MailingDataAccess
Return mailingDA.UpdateCategorize(da, Me)
End Function
MailingDataAccess.UpdateCategorize is defined as:
Public Function UpdateCategorize(ByRef da As DataAccess, ByVal clsMailing As MailingData) As Integer
Dim db As Database = da.Database
Dim DbCommand As DbCommand
Dim dbStatus As Integer = 0
DbCommand = da.Database.GetStoredProcCommand("proc_UpdateCategorize")
With db
.AddInParameter(DbCommand, "#pMailingID", Data.DbType.Int32, clsMailing.MailingID)
dbStatus = db.ExecuteNonQuery(DbCommand)
Return dbStatus
End Function
I figured it out, this line:
dbStatus = db.ExecuteNonQuery(DbCommand)
should be:
dbStatus = db.ExecuteNonQuery(DbCommand, da.Transaction)
Related
Help translate C# code from this link Simplest way to populate class from query in C# to VB Net.
Option Infer On
Imports System.Reflection
Private Sub Main()
Dim connectionString = "..."
Dim records = (New Query(connectionString)).SqlQuery(Of TVChannel)("select top 10 * from TVChannel")
End Sub
Private Class TVChannel
Public Property number() As String
Public Property title() As String
Public Property favoriteChannel() As String
Public Property description() As String
Public Property packageid() As String
Public Property format() As String
End Class
Public Class Query
Private ReadOnly _connectionString As String
Public Sub New(ByVal connectionString As String)
_connectionString = connectionString
End Sub
Public Function SqlQuery(Of T)(ByVal query As String) As List(Of T)
Dim result = New List(Of T)()
Using connection = New SqlConnection(_connectionString)
connection.Open()
Using command = connection.CreateCommand()
command.CommandText = query
Using reader = command.ExecuteReader()
Dim columns = Enumerable.Range(0, reader.FieldCount).Select(Function(f) reader.GetName(f)).ToArray()
Dim properties = GetType(T).GetProperties()
Do While reader.Read()
Dim data = New Object(reader.FieldCount - 1){}
reader.GetValues(data)
Dim instance = DirectCast(Activator.CreateInstance(GetType(T)), T)
For i = 0 To data.Length - 1
If data(i) Is DBNull.Value Then
data(i) = Nothing
End If
Dim [property] = properties.SingleOrDefault(Function(x) x.Name.Equals(columns(i), StringComparison.InvariantCultureIgnoreCase))
If [property] IsNot Nothing Then
[property].SetValue(instance, Convert.ChangeType(data(i), [property].PropertyType))
End If
Next i
result.Add(instance)
Loop
End Using
End Using
End Using
Return result
End Function
End Class
but, I got error on this line
Dim instance = DirectCast(Activator.CreateInstance(GetType(T)), T)
System.MissingMethodException: 'No parameterless constructor defined for this object.'
This is a much better pattern to follow. It addresses at least four issues in the original code (sql injection, Nothing vs null, constructor access, unnecessary allocations):
Public Module SQL
Private ReadOnly _connectionString As String = "..."
Public Iterator Function Query(Of T)(ByVal query As String, translate As Func(IDataRecord, T), ParamArray data() As SqlParameter) As IEnumerable(Of T)
Using connection As New SqlConnection(_connectionString), _
command As New SqlCommand(query, connection)
If data IsNot Nothing Then command.Parameters.AddRange(data)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
Yield translate(reader)
End While
reader.Close()
End Using
End Using
End Function
End Module
Call it like this:
Private Sub Main()
Dim records = SQL.Query("select top 10 * from TVChannel",
Function(r)
'Yes, you're doing the mapping manually now for each query.
'But this lets you properly account for things NULL, column name mismatches, computed properties, etc.
Return New TVChannel With {
.number = r["number"],
.title = r["title"],
.favoriteChannel = r["favoriteChannel"],
.description = r["description"],
.packageid = r["packageid"],
.format = r["format"]
}
End Function,
Nothing)
For Each channel As TVChannel In records
Console.WriteLine($"Channel {channel.number}, {channel.title}")
Next
End Sub
I'm very new to .NET. I am trying to use a code example from the first person that posted a response, here: Connect to remote MySQL database using VB.NET 2010
I would like to instantiate the MySqlVB model object but when I add the following code into the controller, I get a not found error. I don't know how to resolve this.
The error is: Warning 1 Namespace or type specified in the Imports 'MySql.Data.MySqlClient' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
What I need is to run a MySQL query and to return the dataset to the controller. Can someone show me how to do this, please?
I'm using VB 2010 Express to do this.
This is the controller
Public Class Form1
Private Sub PrintBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBtn.Click
Dim data As New MySqlVB
With data
If .Connection Then
MessageBox.Show("Database Conneted.")
Else
MessageBox.Show(.ErrorMessage)
End If
End With
End Sub
End Class
And this is my model object
Imports MySql.Data.MySqlClient
Public Class MySqlVB
Private _connection As New MySqlConnection
Private _errormessge As String
Private _servername As String = "xxx.xxx.xxx.xxx"
Private _databasename As String = "testdb"
Private _userid As String = "theuser"
Private _password As String = "thepass"
Public WriteOnly Property ServerName() As String
Set(ByVal value As String)
_servername = value
End Set
End Property
Public WriteOnly Property DatabaseName() As String
Set(ByVal value As String)
_databasename = value
End Set
End Property
Public WriteOnly Property UserID() As String
Set(ByVal value As String)
_userid = value
End Set
End Property
Public WriteOnly Property Password() As String
Set(ByVal value As String)
_password = value
End Set
End Property
Public ReadOnly Property ErrorMessage() As String
Get
Return _errormessge
End Get
End Property
Public Function Connection() As Boolean
Try
_connection.ConnectionString = "Server=" & _servername & ";Port=3306;Database=" & _databasename & ";User ID=" & _userid & ";Password=" & _password & ""
_connection.Open()
If _connection.State = ConnectionState.Open Then
_connection.Close()
Return True
End If
Catch ex As Exception
_errormessge = ex.Message
Return False
End Try
End Function
End Class
Assuming you have fixed your reference to MySql.Data.MySqlClient I think your class could use some work.
Public Class DataAccess
Private ConnectionString As String
Public Sub New(UserName As String, Password As String)
Dim builder As New MySqlConnectionStringBuilder With {
.Server = "xxx.xxx.xxx.xxx",
.Database = "testdb",
.UserID = UserName,
.Password = Password
}
ConnectionString = builder.ConnectionString
Debug.Print(ConnectionString) 'just to see what the builder created
End Sub
Public Function TestConnecion() As Boolean
Using cn As New MySqlConnection(ConnectionString)
Try
cn.Open()
Catch ex As Exception
Debug.Print(ex.Message) 'just to see what is wrong with connection
Return False
End Try
End Using
Return True
End Function
Public Function GetData() As DataTable
Dim dt As New DataTable
Using cn As New MySqlConnection(ConnectionString)
Using cmd As New MySqlCommand("Select * From SomeTable")
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
Return dt
End Function
End Class
Assuming you have a DataGridView to display data and 2 text boxes for user id and password, you can use your class in your form like this.
Private Sub FillGrid()
Dim daClass As New DataAccess(txtUser.Text, txtPassword.Text)
Dim dt = daClass.GetData
DataGridView1.DataSource = dt
End Sub
Of course you will need to add error handling. Also you need to salt and hash passwords. Plain text passwords should never be stored.
I am trying to create an arraylist of objects, and everything is working fine except that the value of all 11 objects are the same. I've have tried multiple ways of writing the code, but I get the same outcome everytime.
arrFullProdList collects the name of all the products in the database. This is working properly. arrProducts is where I am having the issue with all the objects being the same.
What am I doing wrong?
Declared
Private objReader As SqlDataReader
Private objProducts As New CProducts
Private arrFullProdList As ArrayList = New ArrayList
Public arrProdcuts As ArrayList = New ArrayList
class CProduct
Public Class CProduct
Private _pstrProdId As String
Private _pstrProdDesc As String
Private _psngWhCost As Single
Private _psngRetPrice As Single
Private _pblnTaxable As Boolean
Private _isNewProd As Boolean
Public Sub New()
'_pstrProdId = ""
'_pstrProdDesc = ""
'_psngWhCost = 0
'_psngRetPrice = 0
'_pblnTaxable = False
'_isNewProd = False
End Sub
Public Property strProdId() As String
Get
Return _pstrProdId
End Get
Set(strVal As String)
_pstrProdId = strVal
End Set
End Property
Public Property strProdDesc() As String
Get
Return _pstrProdDesc
End Get
Set(strVal As String)
_pstrProdDesc = strVal
End Set
End Property
Public Property sngWhCost() As Single
Get
Return _psngWhCost
End Get
Set(sngVal As Single)
_psngWhCost = sngVal
End Set
End Property
Public Property sngRetPrice() As Single
Get
Return _psngRetPrice
End Get
Set(sngVal As Single)
_psngRetPrice = sngVal
End Set
End Property
Public Property blnTaxable() As Boolean
Get
Return _pblnTaxable
End Get
Set(blnVal As Boolean)
_pblnTaxable = blnVal
End Set
End Property
Public Property IsNewProd() As Boolean
Get
Return _isNewProd
End Get
Set(blnVal As Boolean)
_isNewProd = blnVal
End Set
End Property
Public ReadOnly Property GetSaveParameters() As ArrayList
Get
Dim paramList As New ArrayList
paramList.Add(New SqlClient.SqlParameter("ProdId", _pstrProdId))
paramList.Add(New SqlClient.SqlParameter("ProdDesc", _pstrProdDesc))
paramList.Add(New SqlClient.SqlParameter("WhCost", _psngWhCost))
paramList.Add(New SqlClient.SqlParameter("RetPrice", _psngRetPrice))
paramList.Add(New SqlClient.SqlParameter("Taxable", _pblnTaxable))
Return paramList
End Get
End Property
Public Function Save() As Integer
'return -1 if the ID already exists and we can't create a new record
If _isNewProd Then
Dim strRes As String = myDB.GetSingleValueFromSP("sp_CheckProdIDExists", _
New SqlClient.SqlParameter("ProdId", _pstrProdId))
If Not strRes = 0 Then
Return -1 'ID NOT unique!!
End If
End If
'if not a new member or it is new and is unique, then do the save (update or insert)
Return myDB.ExecSP("sp_SaveProduct", GetSaveParameters)
End Function
End Class
class CProducts
Imports System.Data.SqlClient
Public Class CProducts
'This class represents the Members table and the associated business rules
Private _Product As CProduct
'constructor
Public Sub New()
'instantiate the CMember object
_Product = New CProduct
End Sub
Public ReadOnly Property CurrentObject() As CProduct
Get
Return _Product
End Get
End Property
Public Sub Clear()
_Product = New CProduct
End Sub
Public Sub CreateNewProduct() 'call me when you are clearing the screen to create a new member
Clear()
_Product.IsNewProd = True
End Sub
Public Function Save() As Integer
Return _Product.Save
End Function
Public Function GetProductList() As SqlDataReader
Return myDB.GetDataReaderBySP("dbo.sp_GetProductList")
End Function
Public Function GetProducIdList() As SqlDataReader
Return myDB.GetDataReaderBySP("dbo.sp_GetProductIdList")
End Function
Public Function GetProductByName(strProdDesc As String) As CProduct
Dim params As New ArrayList
Dim param1 As New SqlParameter("proddesc", strProdDesc)
params.Add(param1)
FillObject(myDB.GetDataReaderBySP("dbo.sp_GetProductByName", params))
Return _Product
End Function
Public Function GetProductById(strProdId As String) As CProduct
Dim aParam As New SqlParameter("ProdId", strProdId)
FillObject(myDB.GetDataReaderBySP("dbo.sp_GetProductByID", aParam))
Return _Product
End Function
Public Function FillObject(sqlDR As SqlDataReader) As CProduct
Using sqlDR
If sqlDR.Read Then
With _Product
.strProdId = sqlDR.Item("ProdId") & ""
.strProdDesc = sqlDR.Item("ProdDesc") & ""
.sngWhCost = sqlDR.Item("WhCost") & ""
.sngRetPrice = sqlDR.Item("RetPrice") & ""
.blnTaxable = sqlDR.Item("Taxable") & ""
End With
Else
'failed for some reason
End If
End Using
Return _Product
End Function
'----------Start Alex's Code---------
Public Function GetProductByDesc(strProdDesc As String) As SqlDataReader
Dim aParam As New SqlParameter("ProdDesc", strProdDesc)
Return myDB.GetDataReaderBySP("dbo.sp_GetProductByDesc", aParam)
End Function
End Class
main
Private Sub LoadProducts()
arrProdcuts.Clear()
objReader = objProducts.GetProductByDesc(txtSearch.Text)
While objReader.Read
arrFullProdList.Add(objReader.Item("prodDesc"))
End While
objReader.Close()
For i = 0 To arrFullProdList.Count - 1
Dim aNewProd As CProduct
aNewProd = objProducts.GetProductByName(arrFullProdList.Item(i).ToString)
arrProdcuts.Add(aNewProd)
Next
End Sub
Using EF with a SQL datastore, the datastore is not updating with the correct data. Here is my code portion for how i create the wrapper object and the relevant function that should update. I think my issue has something to do with the lifecycle of the entity context? But shouldn't re-attaching the object to the context before saving it resolve any issues?
Public Sub New(ByVal UserID As Integer)
Using Context As New RiseDB.RiseDBContainer
MyUser = Context.Users.Find(UserID)
End Using
End Sub
Public Function Save() As Boolean
Using Context As New RiseDB.RiseDBContainer
Context.Users.Attach(MyUser)
Context.SaveChanges()
End Using
Return True
End Function
Protected Overrides Sub Finalize()
If Not IsNothing(Context) Then
Context.SaveChanges()
Context.Dispose()
Context = Nothing
End If
MyBase.Finalize()
End Sub
The function that should set a property:
Public Shared Function PasswordChanged(ByVal UserID As Integer) As Boolean
Dim tmp As New User(UserID)
tmp.IsPasswordReset = False
tmp.Save()
Return True
End Function
I'm new with classes and I want to create a SqlCommandManager class and I can't figure out how to pass SqlParameter on my class.
For example if I want to insert data I would just use my class like client below.
'Client
Dim m_SqlComManager as new SQLCommandManager("MyConnectionString")
m_SqlCommandManager.Commandtext = "INSERT INTO [TableName]([Field1],[Field2])VALUES(#Field1,Field2);"
m_SqlCommandManager.Parameters.AddWithValue("#Field1","SomeValue1")
m_SqlCommandManager.Parameters.AddWithValue("#Field2","SomeValue2")
m_SqlCommandManager.ExecuteNonQuery()
'Here is my class
Imports System.Data.SqlClient
Public Class SQLCommandManager
Private m_SqlParameters As SqlParameter()
Private m_Commandtext As String
Private m_ConStr As String
Public WriteOnly Property SQlParameter() As SqlParameter()
Set(ByVal value As SqlParameter())
value = m_SqlParameters
End Set
End Property
Public Property CommandText() As String
Get
Return m_Commandtext
End Get
Set(ByVal value As String)
value = m_Commandtext
End Set
End Property
Public Sub New(ByVal con As String)
m_ConStr = con
End Sub
Public Sub ExecuteNonQuery()
Using con As New SqlConnection(m_ConStr)
Using com As New SqlCommand
com.Connection = con
com.CommandText = m_Commandtext
'Please help
'How can i insert parameter here from client..
If con.State = ConnectionState.Closed Then
con.Open()
End If
com.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
How how can I set the parameters before the ExecuteNonQuery method?
Thanks in advance..
I would do something like this:
Public Class SqlCommandManager
Private m_SqlParameters As List(Of SqlParameter)
Private m_Commandtext As String
Private m_ConStr As String
Public Sub New()
m_SqlParameters = New List(Of SqlParameter)()
End Sub
Public ReadOnly Property SqlParameters() As List(Of SqlParameter)
Get
Return m_SqlParameters
End Get
End Property
Public Property CommandText() As String
Get
Return m_Commandtext
End Get
Set
value = m_Commandtext
End Set
End Property
Public Sub New(con As String)
m_ConStr = con
End Sub
Public Sub ExecuteNonQuery()
Using con As New SqlConnection(m_ConStr)
Using com As New SqlCommand(m_Commandtext, con)
com.Parameters.AddRange(m_SqlParameters.ToArray())
con.Open()
com.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
End Class
What I've changed:
Changed the class name to SqlCommandManager to be in line with Microsoft's recommendations (don't capitalize more than 2 letters in an abbreviation; IO is fine, Sql and Xml should not be all capitalized)
I would use a List(Of SqlParameter) rather than an array - much easier to deal with, much easier to add additional parameters to it
I prefer to pass the CommandText and the SqlConnection right into the constructor of the SqlCommand - that way,you definitely never forget these two vital bits of information!
Just before your .ExecuteQuery, add the parameters defined in your list to the parameter array of the SqlCommand using a single call to .AddRange()