Hi
I'm trying to execute a SQL command, but I just can't find out why this is not working. This is how I defined the "execute" function in my class named "clsSQL":
''#...
Private m_sConnectionString As String = String.Empty
''#...
Friend WithEvents m_objConnection As SqlConnection
Friend WithEvents m_objCommand As SqlCommand
''#...
Public Function OpenConnection() As Boolean
Try
m_objConnection = New SqlConnection(m_sConnectionString)
m_objConnection.Open()
Select Case m_objConnection.State
Case Data.ConnectionState.Open : Return True
Case Else : Return False
End Select
Catch ex As Exception
RaiseEvent OnError("OpenConnection", ex)
End Try
End Function
Public Function Execute(ByVal sQuery As String) As Boolean
Try
#If DEBUG_MODE Then
Debug.WriteLine(sQuery)
#End If
m_objCommand = New SqlCommand(sQuery, m_objConnection)
m_objCommand.ExecuteNonQuery()
m_objCommand = Nothing
Return True
Catch ex As Exception
RaiseEvent OnError("Execute", ex)
End Try
End Function
''#..
''#...
This is how I'm calling it:
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
strSQL = "INSERT INTO ... blablabla..."
oSQL.Execute(strSQL)
End If
End Using
The code raises no error, it is just not saving the data in the database. The error is not in the SQL command, I've manually tested it ;)
For instance, I can execute perfectly the next function without any kind of problems:
Public Function ToDataGrid(ByVal oDataGrid As DataGridView, _
ByVal sQuery As String, _
Optional ByVal sTable As String = "") As Boolean
Try
#If DEBUG_MODE Then
Debug.WriteLine(sQuery)
#End If
Dim objDataSet As New DataSet
objDataSet = ToDataSet(sQuery, sTable)
oDataGrid.DataSource = objDataSet.Tables(0)
objDataSet.Dispose()
objDataSet = Nothing
Return True
Catch ex As Exception
RaiseEvent OnError("ToDataGrid", ex)
End Try
End Function
And this is how I'm calling it:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
oSQL.ToDataGrid(Me.DataGridView, "select * from table")
End If
End Using
End Sub
Probably, I just need another pair of eyes, 'cause I can't see what am I doing wrong :|
Thanks