Unable to Load DLL 'sqlite.interop.dll' After Create Setup File - c#

I am working on a windows application using Visual studio 2015(Professional) and Sqlite DataBase, windows 8.1(x64). i Install Sqlite From Nuget it Autometically install These References... system.componentmodel.dataannotations, System.Data.SQLite, System.Data.SQLite.EF6, System.Data.SQLite.Linq .
when i execute my app in visual Studio, Everything is working Fine. then Publish it by Windows Setup. then Install The Newly Created Setup File on My machine windows 8.1(x64). Whenever i try to Run it and Click on Login Button it gives the following error
"unable to load dll 'sqlite.interop.dll': the specified module could not be found. (exception from hresult: 0x8007007e)".
my Code is...
string connectionString = #"Data Source = SampelTest.db; Version = 3; new = false; compress = true; ";
private void buttonLogin_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
try
{
using (SQLiteCommand sqlcmd = new SQLiteCommand("select count(*) from [SampelTest]", con))
{
con.Open();
Int32 count = Convert.ToInt32(sqlcmd.ExecuteScalar());
con.Close();
if (count > 0)
{
MessageBox.Show("User have in DataBase");
}
else
{
MessageBox.Show("Empty DataBase");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
}
My Database file in the Debug Folder "SampelTest.db"i already tried these step that are from Google to resolve my problem.
Coppy Sqlite.Interop.dll File From x64 To Debug Folder.
create x86 and x64 folder from solution Explorar and add Sqlite.Interop.dll from x86 and x64 respectively and Set Property, Build Option => Content, Copy to output Directory => Copy always.
Unchake the Prefer 32-bit.
After Try these steps can't able to solve this problem. Please Help to me to solve this. Thanks For Looking.

Related

Any C# WinApplication with Install Sheild Building to .exe with SqlServer Database

i have compile the application to Setup.exe with Install Sheild in Visual studio 2015 with SQL server 2012. The application work on my computer but when i transfer Setup.exe it to clients computer it didnt work generate problem with client computer.
Need A way to handle the application with
I have attached the database to SQLSERVER Managment studio.
I have
change configuration File Data Source=""to client computer Address
but still not working.
public static string GetConnection()
{
return ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
}
SqlConnection con = new SqlConnection(GetConnection());
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select EmpID from RegForm",con);
int i= Convert.ToInt32(cmd.ExecuteScalar());
if(i!=0)
{
MessageBox.Show("value type is " + i);
con.Close();
MessageBox.Show("Connection is closed ");
}
}
I just want that this compile application Start working with setting path in configuration DataSource Attribute nd the programming start working with Active Database

IBM DB2 with .NET Core 2.1

I am having issues connection to IBM DB2 instance with the following nuget package:
https://www.nuget.org/packages/IBM.Data.DB2.Core-lnx/1.2.2.100
Code:
static void Main(string[] args)
{
using (DB2Connection con = new DB2Connection("Server=xxx:8471;Database=xxx;UID=xxx;PWD=xxx"))
{
try
{
DB2Command MyDB2Command = new DB2Command("select * from xxx.xxx", con);
con.Open();
var reader = MyDB2Command.ExecuteReader();
}
catch (Exception ex)
{
throw ex;
}
}
Console.WriteLine("DONE");
}
Result:
Loading the DB2APP from Appication Assembly Entry point location
Please ensure environment variable LD_LIBRARY_PATH value is set to
/opt/app-root/src/bin/Debug/netcoreapp2.1/clidriver/lib ,
/opt/app-root/src/bin/Debug/netcoreapp2.1/clidriver/lib/libdb2.so
LIbrary handle written value is a non zero 15186032
The LD_LIBRARY_PATH is set correctly. A quick printenv shows it is.
Also I start the app with this command:
LD_LIBRARY_PATH="/opt/app-root/src/bin/Debug/netcoreapp2.1/clidriver/lib:/opt/app-root/src/bin/Debug/netcoreapp2.1/clidriver/lib/libdb2.so" dotnet run
I get no errors, the program just hangs forever on the open. I had our database team debug on there end and they see the open connection but can give me no further information. Any ideas?

C# how to connect WPF and SQLITE database easiest way

New to wpf and sqlite. I just want an internal database for my project. I want this project to be able to used by others so I cannot use mysql as a database. So I search for internal databases and decided to try sqlite. Im finding tutorial but they are really confusing. I'm familiar in database I know the queries but now I just need to know what are the necessary things to setup to start the connection between WPF and sqlite. Below is my code(I create a different class for my sqlite connection):
sqlite class
using System.Data.SQLite;
namespace StartMyApps
{
class StartMyAppDb_sqlite
{
public SQLiteConnection myConn;
public SQLiteCommand myComm;
public SQLiteDataReader myReader;
public void openConnection(string query)
{
myConn = new SQLiteConnection("Data Source=StartMyApplication.sqlite;Version=3;");
myConn.Open();
myComm = new SQLiteCommand(query, myConn);
myComm.ExecuteNonQuery();
myReader = myComm.ExecuteReader();
}
}
}
Main class (has a button to trigger the connection and will pass a query)
private void hide_btn_Click(object sender, RoutedEventArgs e)
{
sqliteDB.openConnection("select *from application where app_id='1' and app_name='chrome.exe';");
bool hasAccount = false;
while (sqliteDB.myReader.Read())
{
hasAccount = true;
}
if (hasAccount == false)
{
MessageBox.Show("Logged in");
}
else if (hasAccount == true)
{
MessageBox.Show("Username invalid");
}
}
With this code I got an error says
"An unhandled exception of type 'System.DllNotFoundException' occurred
in System.Data.SQLite.dll Additional information: Unable to load DLL
'SQLite.Interop.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)"
Please help. Any help will be much appreciated THANKS!
I think this is what your looking for: Unable to load DLL 'SQLite.Interop.dll'
Assuming you have:
SQLite installed (correct version for windows)
Appropriate references
Follow below steps:
Add 2 folders in the project and name it x64 and x86.
Add SQLite.interop.dll for x64 and x86 respectively(google for the same)
In SQLite.interop.dll properties, set
"Build Action" --> Content
and "Copy to Output directory" -> Copy Always/Copy if newer.

Referring external DLLs in WPF crashing Application

I have used following 3 SQL Server (Version SQL Server 2008 R2) DLLs in a WPF project:
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
Project works fine on the machine on which it was developed. However if I try to move the .exe to some other machine then application gets crashed.
I have set Copy Local = True for all the 3 DLLs so that during compilation it should copy all the 3 DLLs inside Debug folder. (These DLLs are required to find out the SQL Server DATA Root Directory.)
Following the code which I am trying to execute in my App.
try
{
MessageBox.Show("trying to open connection");
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
con = new SqlConnection(conStr);
con.Open();
MessageBox.Show("connection opened ");
var serverConnection = new ServerConnection(con);
var server = new Server(serverConnection);
var defaultDataPath = string.IsNullOrEmpty(server.Settings.DefaultFile) ? server.MasterDBPath : server.Settings.DefaultFile;
var defaultLogPath = string.IsNullOrEmpty(server.Settings.DefaultLog) ? server.MasterDBLogPath : server.Settings.DefaultLog;
string restoreSql = #"RESTORE DATABASE [MyDB]
FROM DISK = '" + this.FilePath + #"'
WITH
MOVE 'MyDB' TO '" + defaultDataPath + #"\MyDB.mdf',
MOVE 'MyDB_Log' TO '" + defaultLogPath + #"\MyDB_Log.ldf',
RECOVERY, REPLACE, STATS = 10";
SqlCommand restoreCmd = new SqlCommand(restoreSql, con);
int status = restoreCmd.ExecuteNonQuery();
completedFlag = true;
}
catch (Exception ex)
{
MessageBox.Show("Database Restore Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
if (completedFlag)
MessageBox.Show("Database restore successful.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show("SYSTEM ERROR: Failed restoring database.\nPlease restart SQL Server Service and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Not able to find out why my application is crashing.
When distributing certain packages, just including the dll within your package may not help. You need to read the distribution guidelines of those libraries.
In the case of SMO, this is what MSDN says
If you develop an application that uses SQL Server Management Objects,
you need to make sure that the necessary support files are present on
the computer with the application. The SQL Server 2008 feature pack
contains a redistributable package for the SQL Server Management
Objects.
If you have these support packages installed with your external libraries, your problem should be solved.

Executing SQL scripts with GO statements without SMO

One thing I’m having problems with, is deploying app to machine not having SMO installed on it. I want to execute scripts having GOstatements, but also would like to avoid the need for installing SMO package on a target machine or parsing SQL script file (since I don't know if sql script will contain functions or SPs).
I tried manually copying SMO relevenat dlls to bin folder of executable but there are always some other dlls needed to run the app.
Any help with this?
Alternatively to SMO, the sqlcmd utility can understand GO statements. You will need to use System.Diagnostics.Process in order to call out to it with your script as an argument.
By no means am I saying this is the way to do it, but it is a valid alternative. You still have the same issues as before if the utility doesn't exist on the target machine.
You might also be able to deploy SMO with the installer package without "installing" it on the target machine:
Embedding SMO dlls in Setup and deployment
I find below solution well tested
using (SqlConnection cn = new SqlConnection(connectionString))
{
try
{
cn.Open();
FileInfo file = new FileInfo(fileName);
string script = file.OpenText().ReadToEnd();
string[] splitChar = { "\r\nGO\r\n" };
var sqlLines = script.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
int res = 0;
SqlCommand cmd = null;
foreach (var query in sqlLines)
{
cmd = new SqlCommand(query, cn)
{
CommandTimeout = 5400
};
res = cmd.ExecuteNonQuery();
}
cn.Close();
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
cn.Close();
}
}

Categories