I cant open sqlite database - c#

I am required to get sqlitedb in located here
C:\Users\xxx\AppData\Local\Mozilla\Firefox\Profiles\zxl8ocql.default.places.sqlite.
The following code works while accessing Google Chrome data:
private const string PathToChromeData = #"\Google\Chrome\User Data\Default\History";
private const string PathToFirefoxData = #"\Mozilla\Firefox\Profiles\zxl8ocql.default\places.sqlite";
string pathh = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PathToFirefoxData);
using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + pathh + ";Version=3;New=False;;"))
{
connection.Open();
SQLiteDataAdapter adapter = new SQLiteDataAdapter("Select * from moz_places", connection);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
So I slightly changed it for getting Firefox data. However I am getting this exception:
unable to open database file
Then I have checked few similar threads on SO. And realized that Firefox locks this database while its running. And I closed the Firefox even changed the location of database. I checked the connection string, but it seems correct. What is wrong here?

You got a size of 0 KB, because you are searching the history file in the wrong directory.
Replace Local with Roaming and you will get the original places.sqlite file.
The new path would be
C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\zxl8ocql.default\places.sqli‌​te.
Why you found a db of 0 KB in Local?
When you try to open a db file with API, if file does not exist it will create a new empty file. That is why you found a 0 KB sqlite file in local.
Well I am also getting the same issue as described above.
I am looking in to it. I will post a solution if I got.

I am glad that I figured it out and fixed it.
The problem was with the sql version; db was created with a higher version and I was trying to open it with a lower version of sqlite.
It should be handled and at least they should display a proper message.
To open firefox db you require sqlite version 3.8.x.

Related

OleDbConnection error 'External component has thrown an exception' when connecting to Excel file

My WinForms application reads data from an Excel file to a DataTable. On the first call, everything works as it should. On the second call (15 minutes later, regulated by a System.Timers.Timer), I get the following error at the line conn1.Open();:
External component has thrown an exception
I have searched StackOverflow but have not found anything that quite answers this issue. I have read OleDbConnection gets "External component has thrown an exception.", however that suggests checking the build configuration platform in Visual Studio, which in my case must be correct as the code executes correctly on first calling.
Below is my code:
DataTable dt = new DataTable();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Mode=Read;Extended Properties=\"Excel 12.0 XML;HDR=Yes\"";
FileInfo file = new FileInfo(path);
OleDbConnection conn1 = new OleDbConnection(connectionString);
conn1.Open(); //Exception is thrown here on second calling
OleDbDataAdapter da = new OleDbDataAdapter(string.Format("SELECT * FROM [Sheet1$]"), conn1);
da.Fill(dt);
The path is always a new file. The old file is replaced by a new file (with a different file name) on every calling.
I have been struggling with this for a few days now, any help is appreciated.
Just stripped the code down, line by line, and it turns out that removing the following line makes the code work:
FileInfo file = new FileInfo(path);
If anyone has any suggestions as to why this is the case, I would be interested to know.

Unable to read xlsx file through OleDb with the size more than ~1mb

I'd like to use sql bulk copy in order to load data from *.xlsx file to the data base. But, I've faced the problem when file size is more than approximately 1mb. When I try to open OleDbConnection I get an error
No error message available, result code: E_FAIL(0x80004005)
Does anyone have an idea about such behavior?
P.S. If file size is less than mentioned above everything works as expected.
string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(connString);
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + WorkSheetName + "$" + DataRange + "]", oledbConn);
OleDbDataReader dr = cmd.ExecuteReader();
string ProfDbBulkCopyConnString = ConfigurationManager.ConnectionStrings["DbBulkCopyConnString"].ToString();
SqlBulkCopy sb = new SqlBulkCopy(ProfDbBulkCopyConnString);
sb.ColumnMappings.Add("Status", "ActionStatus");
sb.ColumnMappings.Add("Process", "ProcessExec");
sb.DestinationTableName = "dba.Execute";
sb.WriteToServer(dr);
Just a brief description of fixing. For more information I recommend to visit:
https://social.msdn.microsoft.com/Forums/en-US/4d1eeb6d-436d-4595-8645-fde90b2f9b18/oledb-error-opening-large-excel-2007-files-on-web-server?forum=adodotnetdataproviders
Export to excel spreadsheet (XLSX) failing
Microsoft ACE OLEDB connection creating empty Excel when there are 166,110 rows
Essentially, xlsx format is some kind of zip archive with a bunch of xml files. So, first of all ACEOLEDB provider try to unzip all data directly to memory buffer, but in case of large xlsx file provider unable to unzip all data to memory buffer and it forced to create temp file on the hard drive. If user hasn't permission to the folder Content.MSO on hard drive mentioned problem appears.
Path to folder depends on your enviroment. In my case it is C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO (32-bit driver on 64-bit Windosw Server 2008 R2).
So, grant access to Content.MSO for user "IIS AppPool\DefaultAppPool" and problem goes away.

OledDbDataAdapter "Syntax error in FROM clause" exception

I'm using OleDB to import data into grid from text file with extension ".K$$".
Here's some example code:
FileInfo file = new FileInfo(filename);
string connectionString = "";
OleDbDataAdapter adapter;
OleDbConnection con;
connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + file.DirectoryName + ";Extended Properties=\"Text;Format=TabDelimited;\"";
con = new OleDbConnection(connectionString);
con.Open();
adapter = new OleDbDataAdapter(String.Format("SELECT * FROM {0} ", file.Name), con);
adapter.Fill(MyDataTable);
when executing the Fill method it throws the exception. What's wrong with the FROM clause? Thanks
EDIT:
Ok, after some tests I found out that the problem is with the "$" symbols. Maybe it's some reserved symbol ?
Also, if I rename the extension to ".txt" the file got loaded into the grid but it only have 1 column , which means it can't see that there're tabs in the rows.
Another issue is that when I change the file extension to something different than ".txt" (for ex. ".tx") the Fill method throws exception "Cannot update. Database or object is read-only".
OK, I just tried creating an example.K$$, and then tried to connect to it using the same provider as stated through Server Explorer in Visual Studio 2010. Its an Unrecognised format.
I don't think this will ever work.
You may need to look at connecting via a different provider or method.
I think You should look at this link :-
EDIT :
http://www.codeproject.com/Articles/6737/Fill-a-DataSet-from-delimited-text-files
It will allow you to read your txt file into a datable correctly.
Try checking the path of the filename if its correct
Check that neither the directory, nor the filename, contain spaces. If they do, you'll need to escape/quote them.
You'll also need to quote the filename if it contains an extension since the . is not a valid character in the FROM clause. Try FROM [{0}] (though this may not be the correct quoting character for the OleDbDataAdapter).

Reading A Fixed Format Text File

Its been years since I have had to attempt to read a file using either Microsoft Text ODBC Driver or Microsoft Jet OLE DB 4.0 Provider.
So I have the following code
public void Example()
{
string CVS = Application.StartupPath;
string SQL = "SELECT * FROM [MyFile.txt]";
string Connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+CVS+";"+"Extended Properties='text;HDR=Yes;FMT=Fixed;";
OleDbDataAdapter OLE = new OleDbDataAdapter(SQL,Connection);
DataTable Table = new DataTable();
OLE.Fill(Table);
}
When I run the above code I get an "Unexpected Error", I know I am missing something, I am not sure what exactly.
Sources:
http://www.connectionstrings.com/textfile
http://www.connectionstrings.com/Providers/net-framework-data-provider-for-ole-db
http://www.aspdotnetcodes.com/Importing_CSV_Database_Schema.ini.aspx
Any direction would be appreciated.
Let us assume the Schema.ini file is correct.
Remove ' (just prior to 'text;) from the connection string.
In order to resolve the "Could not find installable ISAM", run the following command:
Regsvr32 c:\winnt\system32\mstext40.dll
* Make sure that file is in that folder first. And change WINNT to whatever your windows directory is.
I know this is not a real answer to your question, but i would really rethink about using this architecture to read in a file.
I would really prefer something like CSV Reader, cause it gives you much more power about how the data will be interpreted. Alternative you could also take a look into the FileHelpers.

Exception thrown when opening a Excel spreedsheet from C# stating file is being used by another user

I am currently developing an application that requires an excel spreadsheet, location selected by the user, to be read into a DataTable and then stored in a sql server database.
The application works perfectly in my development environment, however when it is deployed into my production environment an exception is thrown with the following message.
The Microsoft Jet database engine cannot open the file '.xls'. It is already opened exclusively by another user, or you need permission to view its data.
My code to read the excel file is as follows:
OleDbConnection objConn = null;
DataSet objDataset1 = null;
string fileLocation = GetFileLocation();
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + fileLocation
+ ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(sConnectionString);
objConn.Open(); //This is where the exception is thrown
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataTable dt = objDataset1.Tables["XLData"];
Note the application and excel file are on different servers in the same domain.
Digging around various forumns and knowledge bases it would appear that the exception is thrown when the "user" does not have permission to use the file. Although not recommended permissions on the file have been set to Full Access for all users.
Apart from file permissions what else could cause this exception to be thrown?
Not sure if this is helpful but...
The error:
The Microsoft Jet database engine
cannot open the file '.xls'.
seems to point to a file named .xls, as opposed to something named, say myspreadsheet.xls or prodfile.xls.
It may be a long shot, but can you do some debugging to make sure that the file Jet is trying to open actually exists? It may be that the filename is not being constructed properly, for some reason.
Here's a few answers to the same root problem:
Read from Excel using OleDb in a Windows Service?
kill the previous opened excel process before starting the new process for reading the excel.it will solve your problem.
i wrote one function you can call it before opening the xls.
private void kill_excel_process()
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
}

Categories