Hello people I've been struggling to use sqlite in my C#2.0 application and I have finally decided to get rid of assumptions and ask really basic questions.
When I created a database say iagency with table users, from external tools like firefox plugging and another sqladmin tool I can't query it from sqlicommand inside vs2005 it displays System.Data.SQLite.SQLiteException:Sqlite Error no such table users, please be assured that I've made reference to system.data.sqlite installed with SQLite-1.0.61.0-setup
When I do the opposite like create a database and a table from VS server explorer and VS database gui tools it can't be queried neither but can be seen by other tools, but tables created through query from VS using stringbuilder eg create table bla bla. it can be display in a datagrid but none of the tools can see and display that table.
WHAT DO I NEED EXACTLY TO MAKE SQLITE WORK IN MY APPLICATION?
I've tried to add sqlite3.dll of sqlitedll-3_6_14.zip downloaded from sqlite site under section precompiled binaries for windows as reference to my application but it fails with make sure it's accessible an it's a valid assembly or com component.
I downloaded this SQLite-1.0.61.0-setup.exe Ran the installation then I wrote this to access the firefox favorites sqlite db.
using System.Data.SQLite; // Dont forget to add this to your project references
// If the installation worked you should find it under
// the .Net tab of the "Add Reference"-dialog
namespace sqlite_test
{
class Program
{
static void Main(string[] args)
{
var path_to_db = #"C:\places.sqlite"; // copied here to avoid long path
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_db + ";Version=3;New=True;Compress=True;");
SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();
sqlite_connection.Open();
sqlite_command.CommandText = "select * from moz_places";
SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();
while (sqlite_datareader.Read())
{
// Prints out the url field from the table:
System.Console.WriteLine(sqlite_datareader["url"]);
}
}
}
}
Try opening up the database in the command line SQLite tool (from SQLite.org), and check the schema.
You can check the schema in this way:
.schema
This will dump out all the SQL necessary to create the tables in the database. Make sure the table is there, with the name you assume it should have.
You do not need the .dll file from SQLite.org, all you need is the assemblies from System.Data.SQLite.
For me - this link helped a lot at start.
Was harder to get subsonic work, to make database accessible through web application -
but that's another story.
You might try adding the location of the assembly and the db to the Path environment variable. The SQLite assembly contains both .Net and native code merged together, so you do not need the C dll. (the mergebin tool they include to do this is pretty interesting)
I also tried adding the location to Path environment variable but without success.
Finally I copied System.Data.SQLite.dll and System.Data.SQLite.lib into the bin folder of the Web application where other assemblies are located, and application worked.
Related
I need to create and access a spatialite-extended SQLite database under x64 windows.
I have downloaded the latest version 1.0.92.0 called sqlite-netFx45-static-binary-bundle-x64-2012-1.0.92.0.zip of System.Data.SQLite. It is referenced from my Visual Studio (2012) project, and seems to work just fine by itself.
I also have the latest precompiled x64 spatiaLite version 4.1.1 called spatialite-4.1.1-DLL-win-amd64.zip
All the dlls from spatialite are present in the executing directory.
When I try to load the extension:
using (var conn = new SQLiteConnection("Data Source=\"" + _sqLiteFullName + "\""))
{
conn.Open();
conn.EnableExtensions(true);
conn.LoadExtension("libspatialite-4.dll");
...
}
I get an AccessViolationException (Attempted to read protected memory. This is often an indication that other memory is corrupt) on the LoadExtension() line.
I notice when looked at with PE Deconstructor (software that determines the bitnewss of dll/exe), it says that my copy of System.Data.SQLite.dll (from the x64 package) is actually 32bits. Is that the problem?
How to I remedy this?
How has anyone else got spatiaLite working on x64?
Actually, the problem could be in spatialite-4.dll this autmun I spent a week trying to fix the same issue without success. It looks like that there problems in spatialite-4.dll (I mean this one downloaded form gaia-sins (official spatialite site) )
You can try to build a Spatialite from sources (like a nightmare (: ) or try to look for another build of .dll. Second option helped me.
Btw, there a couple of .dlls you need to use Spatialite extension:
libsqlite3-0.dll
libgeos-3-0-2.dll
libgeos-c-1.dll
libiconv2.dll
libproj-0.dll
libvirtualtext-2.dll
libspatialite-2.dll
<----- Spatialite v.2 completely suits my project. As I told, if you need v.4 you can try to build it or look for a another build.
Hope, this helps
download mod_spatialite from the site, choose mod_spatialite-4.2.0-win-amd64.7z. unzip and copy all dll to the bin folder of your program.
sample code:
//SELECT load_extension("mod_spatialite") // doesn't need the '.dll' suffix.
using (var cnn = new SQLiteConnection(connStr))
{
//connStr = "FullUri=file::memory:?cache=shared;Pooling=True;Max Pool Size=200;";
cnn.Open();
//cnn.EnableExtensions(true);
using (SQLiteCommand mycommand = new SQLiteCommand("SELECT load_extension(\"mod_spatialite\")", cnn))
{
mycommand.ExecuteNonQuery();
}
Have a look in this Google Groups discussion here:
https://groups.google.com/forum/#!topic/spatialite-users/u2QZpQL_6ek
The latest solution is by Dominik:
I just found out, that the hack described at
http://blog.jrg.com.br/2016/04/25/Fixing-spatialite-loading-problem/
only works with the dlls from the second most recent version of
mingw64 mingw-w64-bin_x86_64-linux_20131228.tar.bz2 from
http://netassist.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_x86_64-linux_20131228.tar.bz2.
Any attempt to do the same with the most recent version available at
sourcefourge
http://sourceforge.net/projects/mingw-w64/files/latest/download
failed on my system.
However, I can definitely confirm, that I can load mod_spatialite with
MyConnection.LoadExtension("mod_spatilite");
My Visual studio, on opening a certain project, builds so fast but takes forever to start running. I searched and found it is saying something like: "Adding a synonym to the database project" is the root cause.
I tried to re-add the data connection from Server Explorer. However, there used to be a long dropdown list to select the Data Source Name, but now only (local Database).....
Could anyone help please....
There are 3 oracles installed on my VM.... and the one I am using, it actually maps to a version which does not have C:\oracle\product\11.2.0\client_1\network\admin/tnsnames.ora and another file.
I got a new VM machine, and it's solved.
I have created a simple and basic Hello World template with the sample coding I found in OfficeWriter. However, I want to save the output in a folder inside the computer, not store online (web)
The website have already stated how to save to a folder but it is not working in my case. Anyone can help on this? By the way, I used a console application to do the coding.
The error mentions that I must add System.Web reference which I think it is not necessary since I am not doing a web or something.
using SoftArtisans.OfficeWriter.ExcelWriter;
namespace ConsoleApplication1
class Program
{
static void Main(string[] args)
{
ExcelTemplate XLT = new ExcelTemplate();
XLT.Open(#"C:\Users\administrator\Desktop\Hello World.xlsx);
DataBindProperties dataProps = XLT.CreateDataBindingProperties();
string value = "Hello World";
XLT.BindCellData(value, "DataValue", dataProps);
XLT.Process();
XLT.Save("Output.xlsx"); //this coding is giving me problem.
}
}
Note: I work for SoftArtisans, makers of OfficeWriter.
Although most of our customers use OfficeWriter in .NET web applications, OfficeWriter can be used in any type of .NET application.
All OfficeWriter objects (ExcelTemplate, ExcelApplication, WordTemplate, and WordApplication) have four output options:
Save directly to disk
Save to System.IO Stream
Stream the generate file to the client as an attachment
Stream the generated file to the client to be viewed in the browser. This only works for Internet Explorer and if viewing Office files in IE is enabled.
The Save method has a dependency on System.Web due to the Save() overloads that use the HttpResponse object. I know customers have run into trouble with the dependency if they were using the .NET 4 client profile because a reference to System.Web is not included automatically. I believe the same is also true for projects like console or forms applications.
To save a file to a particular folder on disk, you will need to provide the full file path to the location on disk. For example "C:\Reports\SampleReport.xlsx". You can use .NET code to help resolve the full file path before passing that value to OfficeWriter.
Here are a couple posts I found that discuss how to get the full file path from a .NET console application:
How can I get the application's path in .NET in a console app?
How to get a path from a directory in a C# console application?
According to the ExcelTemplate.Save() documentation, the file is being saved to the server, even though you do not think it is. Since it is trying to save to the web server, System.Web is needed to resolve the physical path on the server.
You are using the wrong tool then. Directly from the documentation of Excel Writer 7...
SoftArtisans ExcelWriter is a high-performance pure .NET solution that
generates native Microsoft Excel spreadsheets on a Web server. A few
simple lines of code generate editable presentation-quality
spreadsheets that can be saved on the server or viewed instantly by
thousands of concurrent users.
http://wiki.softartisans.com/pages/viewpage.action?pageId=3114609
Look into using Visual Studio Tools for Office or some other library to satisfy your local Excel needs.
You should try adding the System.Web reference to see if that fixes your problem. The System.Web assembly is already installed with the full .NET framework, so it doesn't change anything if you reference it or not. Maybe it will detect that you're not in a web application and save it anyway, but it needs the reference to do so.
Alternatively, use the open source EPPlus project to create Excel spreadsheets and System.Web will not be required - http://epplus.codeplex.com/
Okay, I found the answer to that problem, I just have to add the web reference to the project and it worked like magic. Once again I thank you all for the help rendered.
I'm able to run an asp.net application using a console host to host my services. This works fine.
When I install the services to a server and then attempt to access them with my application, I get an error when running my search function (it's a search application) I used to be doing it using a stored procedure that is dynamically created which was using Microsoft.SqlServer.Management.Sdk.Sfc.dll to create a sql server object and create a stored procedure. Now I'm doing it without a stored procedure and so I no longer need this reference. However, when I try to run my search function with the services installed on the server, it keeps failing because it can't find this file. I've emptied all my bin and obj directories, cleaned the services solution and the asp.net application solution, but it keeps looking for this reference.
I even searched my computer for this file and it was only found in its original location (C:\Program Files\MicrosoftSqlServer\100\SDK\Assemblies). I don't know what else to do to tell my program I no longer care about this reference.
Please help if you can! Thanks very much in advance.
The assembly may still be referenced in your web.config file.
Did you remove the reference to that DLL in your References?
I went through MSDN pages to learn ADO.Net using Commands. I am able to read using the sample code posted there.
But when I tried to use the modification code below, the insert is not happening. I am not ale to figure out why. Can someone please tell me what is wrong with this code?
string connectionString = "A_VALID_CONNECTION_STRING";
string commandText =
"INSERT INTO Contacts (FullName, Mobile) VALUES ('Pierce Brosnan', '1800-007')";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
SqlCommand command = new SqlCommand(commandText, connection);
Console.WriteLine(command.ExecuteNonQuery());
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Edit
No exception is thrown.
The ExecuteNonQuery() which is supposed to return the no. of rows affected is returning 1.
Environment: Visual C# 2010 Express | SQL Server 2008 Express | Windows 7 Ultimate 32-bit.
Update
Previously I was using a MDF file present in the project. It was, I guess, automatically attached to the SQL server instance each time the project ran. This is when I had the problem. The connection string had some info about attaching a database file.
I removed the SQL Server 2008 Express that I installed along with Visual C# 2010 Express. Also removed the MDF file from the project.
I Separately downloaded and installed SQL Server 2008 Express along with Management Studio Express.
Created a new database in management studio.
Used a different type of connection string to use the database in the server.
Now INSERT is working!
P.S. I guess I should have mentioned that I had an attach database file scenario. Really sorry for that.
My suspicion is that you had the following scenario:
Database.mdf file was present in the project with the table structure created in it
Your connection string looked something like this Server=.\SQLExpress;AttachDbFilename=database.mdf;Database=dbname; Trusted_Connection=Yes;, i.e. loading the database in the connection string.
What was happening was, when you built/ran your project, your application was compiled and the database.mdf file was copied along with it to ApplicationProjectFolder\bin\Debug, so that when the application was run, the file database.mdf was present. This means that everytime you ran your project, the "empty" database.mdf file was copied from ApplicationProjectFolder\database.mdf to ApplicationProjectFolder\bin\Debug\database.mdf, hence the data "disappearing". Also, the file database.mdf probably had "Copy Always" set on its properties in the project.
So, the "INSERT" was working, it was just being "reset" everytime you ran your application.