SQLite runs during development, not after complied - vb.net

I'm writing a small app for managing a backend file for a Neo4j project. The app is a VB.NET Windows Form project. It uses an SQLite database. The database connection and functionality of read, update and insert works in Visual Studio during development. But when it is compiled either as *.msi or for CDROM if crashes as soon as the database access is attempted with an error,
Unable to load DLL 'SQLite.Interop.dll'
I'm not sure why this would need interop. But perhaps because I'm not referencing my database properly for the compile. The db is in the release folder.
I've used NUGet to import references including the SQLite 3.13.0.
The code that works within VS is (Q is the query):
Public Class DataLib
Public Shared SQLiteConn As SQLiteConnection
Public Shared SQLitecnStr As String = ""
Public Shared SQLitecmd As SQLiteCommand
Public Shared myAdapter As SQLiteDataAdapter
Public Shared Sub OpenConnection(Q As String)
If SQLiteConn Is Nothing Then
SQLitecnStr = "Data Source=" & My.Application.Info.DirectoryPath & "\db\gfg.sqlite3"
Dim SQLiteConn As New SQLiteConnection
SQLiteConn.ConnectionString = SQLitecnStr
SQLitecmd = New SQLiteCommand(Q, SQLiteConn)
SQLiteConn.Open()
myAdapter = New SQLiteDataAdapter(SQLitecmd)
Else
If SQLiteConn.State = ConnectionState.Open Then
'do nothing
Else
SQLitecnStr = "Data Source=" & My.Application.Info.DirectoryPath & "\db\gfg.sqlite3"
Dim SQLiteConn As New SQLiteConnection
SQLiteConn.ConnectionString = SQLitecnStr
SQLitecmd = New SQLiteCommand(Q, SQLiteConn)
SQLiteConn.Open()
End If
End If
End Sub
SQLLite.Interop on NUGet is for a prior version of VS .NET Framework. I'm using v 4.7.2.I've tried adding SQLite Core from NUGet, but this does not resolve the issue.

Related

'Fill: SelectCommand.Connection property has not been initialized.'

I am using a visual studio 2022 vb.net and mssql management studio.
login form
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles Btnlogin.Click
cn.Open()
cm = New SqlClient.SqlCommand("select * from table_user where username like '" & username.Text & "' and password like '" & password.Text & "'and usertype= '" & usertype.SelectedItem & "'", cn)
dr = cm.ExecuteReader
sda.Fill(dt)
If (dt.Rows.Count > 0) Then
MessageBox.Show("You are login as " + dt.Rows(0)(2))
If (usertype.SelectedIndex = 0) Then
Dim a As New dashboard
a.Show()
Me.Hide()
Else
Dim b As New Admin
b.Show()
Me.Hide()
End If
Else
MessageBox.Show("Username or Password Incorrect. CONTACT ADMINISTRATOR!")
End If
cn.Close()
End Sub
Module
Imports System.Data.SqlClient
Module Module1
Public cn As New SqlConnection("Data Source=DESKTOP-7POF5HE\SQLEXPRESS;Initial Catalog=dict;Integrated Security=True")
Public cm As New SqlCommand
Public dr As SqlDataReader
Public sda As SqlDataAdapter = New SqlDataAdapter(cm)
Public dt As DataTable = New DataTable()
End Module
CAN YOU HELP ME TO SOLVE THIS?
Seems there are several issues with this code.
In your module, you create the command object and the data adapter object. But in the form method, you create a new command object. But that will not update the data adapter to use that new command object. Class variables are reference types. They just point to an object in the memory somewhere. Your cm variable will point to the new command object, but your sda object will internally still point to the old command object.
Furthermore:
You are using both a data reader and a data adapter. Both have their pros and cons, but you probably don't need (or want) to use them both at the same time. I assume you want to use the data adapter. So you can drop the dr = cm.ExecuteReader line in the form method.
Since you will probably always want to create command objects and data adapter objects on the fly (as I would), you could remove them from the module. Just create them both as local variables in your form method.
Try to use the Using statement for such objects. They need to be disposed of nicely when the form method finishes. Otherwise they might keep valuable system resources in use until the .NET garbage collector disposes them (which will probably occur when you close your application, not earlier).
Also be careful with concatenating SQL statements from variables. What would happen here if you enter this text in your username textbox?: ';delete from table_user;--
Hint: Do not actually try it! It will try to delete all your users from the database table table_user. Just try to manually reproduce the SQL string that will be built in your form method. This is called an SQL injection attack. To avoid such nasty things, it's easiest to use SQL parameters in your SQL statements and pass them separately to your command object.

Revit API, Access Database, Attempted to read or write protected memory

I recently upgraded from R16 to R19, but my addin throws an error I can't resolve.
When I try to read an access.mdb file, I get the "attempted to read or write protected memory" error.
I use the exact same code to connect to this file in an Acad2019 dll, there it works fine. Same happens on a different machine.
Windows 10, x64, .Net 4.7.2
The crash occurs in myConnection.open (Attempted to read or write protected memory. This is often an indication that other memory is corrupt.)
Imports System
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class Test
Implements IExternalCommand
Public Function Execute(ByVal revit As ExternalCommandData, ByRef message As String, _
ByVal elements As ElementSet) As Autodesk.Revit.UI.Result _
Implements IExternalCommand.Execute
Dim myConnection As OleDbConnection = New OleDbConnection()
Dim strFileName As String = "C:\Users\X\Desktop\Revit 2019\Database.mdb"
If Not myConnection.State = ConnectionState.Open Then
myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; " & "Data Source=" & strFileName
Try
myConnection.Open()
Catch ex As System.Exception
MsgBox("Error in reading database!")
End Try
End If
Return Autodesk.Revit.UI.Result.Succeeded
End Function
End Class
Any suggestions?
Gr, Piet
Fixed.
I had Office 2016 installed, but also had to install Microsoft Access Database Engine 2010 Redistributable. After that the problem was gone.

Getting DATA from SQL Server by WCF Services using ADO.NET VB.NET

I have attached WCF service to my sample website.
WCF functions work fine except one which use SQL query to get data from SQL server.
I'm not very familiar with VB.NET, my experience comes from VBA, it's similar but setting query is quite different.
At first I tried to use SqlDataReader then SqlDataAdapter - The same results. My Service has stuck.
Visual Studio shows error that SQL Server can't pass data because there is an internal error.
This is strange because when I use "WCF Test Client" in Visual Studio, then both functions work good and receive correct data. Also when I have attached this functions directly to my website also worked good. The problem is using them by WCF.
Below is my function with SQLDataAdapter
Public Function GetCookiesPriceDS(ByVal nameOfCookie As String) _
As DataSet Implements IService1.GetCookiesPriceDS
Dim queryString As String
Dim dataSet As DataSet = New DataSet("temporary")
queryString = "select CookiesPrice from " &
"tblCookies where CookiesName='" & nameOfCookie & "'"
Using connection As New SqlConnection _
("Server= xyz\SQLEXPRESS; Database = Cookies2; " &
"Integrated Security = true;User Id = xyz;Password = xyz")
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand(queryString, connection)
adapter.Fill(dataSet)
Return dataSet
End Using
End Function
Ok, I have found some sample code in vb.net and have done some small changes. It doesn't work because in "WCF Test Client" red symbol appears next to function, with comment - "This operation is not supported in WCF Test Client because it uses type WcfService.CookiesData" But despite that I think this code is much better than my previous version and has better definition in and . But still there is some problem and I can't figure out what is wrong
Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Imports System.ServiceModel
Imports System.Runtime.Serialization
Public Class Service1
Implements IService1
Public Function GetCookiesPriceDS(ByVal nameOfCookie As String) _
As CookiesData Implements IService1.GetCookiesPriceDS
Using con As New SqlConnection("Server= xyz\SQLEXPRESS; Database = Cookies2; " &
"Integrated Security = true;")
Using cmd As New SqlCommand("select CookiesPrice from tblCookies where CookiesName='" & nameOfCookie & "'")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
Dim ck As New CookiesData()
sda.Fill(ck.CustomersTable)
Return ck
End Using
End Using
End Using
End Using
End Function
End Class
<ServiceContract()>
Public Interface IService1
<OperationContract()>
Function GetCookiesPriceDS(ByVal nameOfCookie As String) As CookiesData
End Interface
<DataContract()>
Public Class CookiesData
Public Sub New()
CustomersTable = New DataTable("TblCookies")
End Sub
<DataMember()>
Public Property CustomersTable() As DataTable
End Class

Creating Connections to ODBC Data Sources using codes in vb.net

I am trying to connect using vb.net (Visual studio 2013) to an MS Access Database 2007(.accdb) using codes. But something is wrong in my code and i can't figure it out.
the Database name is "localDatabase.accdb"
I didn't put any password on my database
I'm using a 64bit
Thanks in advance!
Here is my code:
Module Module1
Dim conn As New System.Data.Odbc.OdbcConnection
Public Sub ConnectToOdbc()
conn.ConnectionString = Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to data source")
Finally
conn.Close()
End Try
End Sub
End Module
this is just a hypothesis..
I am using ACE 12.0 now on my app, and it works fine. the different thing is the code try to use this..
Imports System.Data.OleDb
'instead of obdc
con = New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " & Path.Combine(Application.StartupPath, "YourDatabase.accdb")
'put your .accdb in your app folder
maybe the error is it can't find your .accdb file.
Change this line:
conn.ConnectionString = Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
to this:
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MyPc\Documents\Visual Studio 2013\Projects\database\localDatabase.accdb"
ConnectionString as the name tell is a string, so you need to wrap it with double-quotes to be recognized as string by VB compiler.
UPDATE :
Also you need to change the connection object from Odbc to OleDb:
Dim conn As New System.Data.OleDb.OleDbConnection

connectionstring in visual studio 2010

I am kind of new to programming, i am doing a vb.net database project in which i am using access database as my backend database. I am using visual studio 2010.
I have used a folder browser dialog to get the directory where my access .accdb file is. I have used a string to get the the strings together to form a connectionstring. My problem is how can i make the program to wait for me to select a location to complete the connectionstring? how can i prevent the program from looking up the connectionstring in "my settings" tab in my project page....
Also using add datasource wizard,when deployed gives error that db file is not found in the displayed location, and if i check that location i see two files and the db is present in one file but the displayed location is other file.
Private Sub SettingToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingToolStripMenuItem.Click
FolderBrowserDialog1.ShowDialog()
Connectstring = FolderBrowserDialog1.SelectedPath()
connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Connectstring & "\QuestionPool.accdb;"
connection = New OleDbConnection(connectionstring) 'is not needed. garbage code.
writer = New StreamWriter("Connectionstring.connection")
writer.WriteLine(connectionstring)
writer.Close()
End Sub
also I can't get the
connection = new oledbconnection(connectionstring)
to work.
I have to include home. connectionstring in the dataset.designer.vb to get the connection work.
Also how do I add the connection string variable to my.settings tab?