Windows Phone 7: SQLite - c#

I'm trying to get Community.Csharp working with Windows Phone, I've tried using both the version from http://wp7sqlite.codeplex.com/ and compiling the main trunk with the WINDOWS_PHONE flag, but when I run the application on the phone I get an error when trying to execute any queries (but not when opening the database; only on queries): "Unable to open database file"
_conn = new SqliteConnection("Version=3,uri=file:recipes.sqlite");
_conn.Open();
cmd.CommandText = "SELECT * FROM recipes";
SqliteDataReader reader = cmd.ExecuteReader();
Interestingly though, I'm using the following to check for the existence of a table and no exceptions are thrown:
cmd.CommandText = "SELECT * FROM sqlite_master WHERE name='" + tableName + "'";
SqliteDataReader rdr = cmd.ExecuteReader();
exists = rdr.Read();
rdr.Close();
I have a Windows app which uses SQLite, so if I could use SQLite as opposed to Sterling DB or something else, that would save huge amounts of time. The problem I'm having currently is that once I open the database and close it, I cannot re-open it.

I am using the same library for our company's application and as far as I know, also documented at http://wp7sqlite.codeplex.com (under Some Recommendations ), if you close the connection you'll need to recreate it again.
== ADDITIONAL COMMENTS ==
I've tracked down the cause of the error, created a fix and am testing it in our application. Briefly, in order to port the Community.CSharpSqlite library to WP7, the author wrote a FileStream wrapper around WP7 IsolatedStorageFileStream. When a db is opened, the db file stream is opened and read and closed by CSharpSqlite. But a handle to this stream is also stored in a Dictionary mapping the file path to stream. When a db is opened for a second time, the handle to the stream is retrieved but since it's closed (I'm assuming, haven't verified yet) the db fails to open.
I will attempt to get my changes deployed to the wp7sqlite.codeplex.com project, but in the meantime if you have the source code make the following changes to Community.CsharpSqlite.FileStream
change from
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int unused)
{
IsolatedStorageFileStream handler = null;
if (FileStream.HandleTracker.TryGetValue(path, out handler))
{
_internal = handler;
}
else
{
if (mode == FileMode.Create || mode == FileMode.CreateNew)
{
_internal = IsolatedStorageIO.Default.CreateFile(path);
}
else
{
_internal = IsolatedStorageIO.Default.OpenFile(path, FileMode.OpenOrCreate);
}
FileStream.HandleTracker.Add(path, _internal);
}
}
to
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int unused)
{
IsolatedStorageFileStream handler = null;
if(FileStream.HandleTracker.TryGetValue(path, out handler))
{
_internal = handler;
if(!_internal.CanRead)
{
FileStream.HandleTracker.Remove(path);
CreateOpenNewFile(path, mode);
}
} else {
CreateOpenNewFile(path, mode);
}
}
private void CreateOpenNewFile(string path, FileMode mode)
{
if(mode == FileMode.Create || mode == FileMode.CreateNew)
{
_internal = IsolatedStorageIO.Default.CreateFile(path);
} else {
try {
_internal = IsolatedStorageIO.Default.OpenFile(path, FileMode.OpenOrCreate);
} catch(Exception ex) {
var v = ex;
}
}
FileStream.HandleTracker.Add(path, _internal);
}
This is the first time I'm attempting to debug and contribute to an open source project. Any comments or thoughts on these changes will be greatly appreciated.
Alasdair.

Hi I have encountered the same issue ... I think i got the fix for it.
This is what I've done.
public void CloseDB()
{
Connection.Close(); //Connection is a property(of type SqliteConnection) of my object
FileStream.HandleTracker.Clear(); //This here is the fix
}
I don't really need to change the dll.
I'm not yet sure if this will cause errors later on , but for now it works for me.
... I'm only a junior programmer:D

Related

KB4022726 breaks windows search query over OleDb

We are using windows search to search for document on a remote share, after applying security update KB4022726 this functionality breaks.
Our implementation is using oledb connector for windows search in C#
Are there any workarounds besides uninstalling KB4022726?
Update:
CVE-2017-8543 might be related.
Sample program - that failes efter install of KB4022726
using System;
using System.Data.OleDb;
namespace windowssearchtest
{
class Program
{
static void Main(string[] args)
{
var computer = "searchserver";
var filepath = #"documents";
var query = $#"
Select System.Itemname
FROM {computer}.systemindex
WHERE SCOPE='file:\\{computer}\{filepath}'";
const string ConnectionString = "Provider=Search.CollatorDSO;Extended Properties=\"Application=Windows\"";
OleDbDataReader myDataReader = null;
OleDbConnection myOleDbConnection = new OleDbConnection(ConnectionString);
OleDbCommand myOleDbCommand = new OleDbCommand(query, myOleDbConnection);
myOleDbCommand.CommandTimeout = 180;
try
{
myOleDbConnection.Open();
myDataReader = myOleDbCommand.ExecuteReader();
if (myDataReader != null && myDataReader.HasRows)
{
Console.WriteLine($"HasRows: {myDataReader.HasRows}");
while (myDataReader.Read())
{
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
Error:
System.Data.OleDb.OleDbException (0x80004005): Uspecificeret fejl
ved System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
ved System.Data.OleDb.OleDbDataReader.GetRowHandles() ved
System.Data.OleDb.OleDbDataReader.ReadRowset() ved
System.Data.OleDb.OleDbDataReader.Read() ved
windowssearchtest.Program.Main(String[] args) i
C:\projects_local\windowssearchtest\windowssearchtest\Program.cs:linje
48
Update 2017-06-26
I have also reproduced this error on a local machine running win10 - 1703
Windows search still works, but will throw an error when you reach the end of the resultset, or if empty on .ExecuteReader()
We made a hack to work around this, i will not recommend this:
//pseudo code
while (Wrap(myDataReader))
<snip>
function Wrap(myDataReader)
{
try
{
return myDataReader.Read();
}
catch (ex)
{
if(ex.HResult == -2147467259) return false; //0x80004005
throw;
}
}
It looks like the issue has been fixed in the latest Optional Updates from MS:
Win7, Server 2008:
https://support.microsoft.com/en-us/help/4022168/windows-7-sp1-windows-server-2008-r2-sp1-update-kb4022168
Win 8.1, Server 2012:
https://support.microsoft.com/en-us/help/4022720/windows-8-1-windows-server-2012-r2-update-kb4022720
Have installed these updates on my test servers (2008, 2012) and not getting that error now!

MS Access OleDB object reference not set to an instance of an object

I have created a .Net 4.5 application that reads and writes to MS Access database. While running the application on my machine (which has MS office) everything is fine but when I run this on my server (which does not has MS Office but has ACE ) I get the object reference not set to an instance of an object.
This happens when I click the button. Following is the code
try
{
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
est_index = getTableIndex(conn,estimateTableIndex);
for (int i = 0; i < inputValues.Count; i++)
{
string FieldName = "";
string FieldValue = "";
if (inputValues[i] is System.Web.UI.WebControls.TextBox)
{
FieldName = inputValues[i].ClientID;
FieldValue = ((System.Web.UI.WebControls.TextBox)(inputValues[i])).Text;
}
else
{
FieldName = inputValues[i].ClientID;
FieldValue = ((System.Web.UI.WebControls.DropDownList)(inputValues[i])).SelectedValue;
}
//everything fine till here..then throws the error below
string my_querry = "INSERT INTO Estimate VALUES(#Est_id,#FieldName,#FieldValue)";
OleDbCommand cmd = new OleDbCommand(my_querry, conn);
cmd.Parameters.AddWithValue("#Est_id", est_index);
cmd.Parameters.AddWithValue("#FieldName", FieldName);
cmd.Parameters.AddWithValue("#FieldValue", FieldValue);
cmd.ExecuteNonQuery();
}
conn.Close();
}
catch (Exception e)
{
Console.Write(e.InnerException.Message);
}
Not sure why this is happening ?
EDIT: I created a virtual machine and downloaded the ACE and the same error occurred that clients face
EDIT 2: The connection string looks like "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\cohber.accdb"
I tried to debug and see where the code broke and it broke at cmd.ExecuteNonQuery();. The message at exception was operation must use an updateable query
Edit 3: the MS Access file is located in c driver. I gave all the users full permission but still get the error
You need to set read/ write permissions to the folder containing the access .mdb file

NpgsqlCopyIn fails by timeout ("CommandTimeout" setting ignored)

I have a quite large dataset (900K records, 140Mb disk space) stored in CSV file in a client app (.NET 4.0). I need to load this data to Postgres 9 db the fastest way. I use Npgsql "NpgsqlCopyIn" technique (Npgsql library version=2.1.0).
For a probe load (138K) insertion works fine - it takes about 7 secons.
But for the whole batch (900K), the code throws timeout exception:
"ERROR: 57014: canceling statement due to statement timeout"
The stack trace is:
Npgsql.NpgsqlState.d_9.MoveNext() at
Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector
context) at Npgsql.NpgsqlCopyInState.SendCopyDone(NpgsqlConnector
context) at Npgsql.NpgsqlCopyInState.StartCopy(NpgsqlConnector
context, NpgsqlCopyFormat copyFormat) at
Npgsql.NpgsqlState.d_9.MoveNext() at
Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector
context) at
Npgsql.NpgsqlConnector.ProcessAndDiscardBackendResponses() at
Npgsql.NpgsqlCommand.ExecuteBlind() at Npgsql.NpgsqlCopyIn.Start()
I tried setting CommandTimeout to kilo values(>7200), zero; tried same values for connection "Timeout" parameter. Also I was trying to set "CommandTimeout" via connection string, but still with no result - "ERROR 57014" comes out again and again.
Please, help to load the batch correctly!
Here is the code I use:
private static void pgBulkCopy(string connection_string, FileInfo fiDataFile)
{
using (Npgsql.NpgsqlConnection con = new Npgsql.NpgsqlConnection(connection_string))
{
con.Open();
FileStream ifs = new FileStream(fiDataFile.FullName, FileMode.Open, FileAccess.Read);
string queryString = "COPY schm.Addresses(FullAddress,lat,lon) FROM STDIN;";
NpgsqlCommand cmd = new NpgsqlCommand(queryString, con);
cmd.CommandTimeout = 7200; //7200sec, 120 min, 2 hours
NpgsqlCopyIn copyIn = new NpgsqlCopyIn(cmd, con, ifs);
try{
copyIn.Start();
copyIn.End();
}catch(Exception ex)
{
Console.WriteLine("[DB] pgBulkCopy error: " + ex.Message );
}
finally
{
con.Close();
}
}
}
Npgsql has a bug regarding command timeout and NpgsqlCopyIn handling.
You may test our current master where we had a lot of fixes about command timeout handling.
You can download a copy of the project in our GitHub page: https://github.com/npgsql/Npgsql/archive/master.zip
Please, give it a try and let us know if it works for you.

File in use by another process error when uploading a file using server but not localhost

I am fairly new to MVC and .NET, and I've hit my first problem that I have been stuck on for 3 days now. I apologize for the lengthy post.
Part of the project I am working on requires that the user be able to select an XLS or XLSX file and upload it so the data can be imported by the application. It is likely the files being uploaded will have upwards of 20,000 rows of data.
I have test files with 5000, 10000, and 20000 rows in them. When I run my app on my local machine (using Visual Studio 2010), all of these files get saved to the network share and processed just fine. No errors. However, when I deploy the app to our development server, I get an error after 5 minutes that the 10k and 20k file cannot be accessed because it is in use by another process.
Error message: The process cannot access the file 'removed this part of path\TestBook-10k-rows.xlsx' because it is being used by another process.
Stack trace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at System.Web.HttpPostedFile.SaveAs(String filename)
at System.Web.HttpPostedFileWrapper.SaveAs(String filename)
at edtStar.Controllers.TableController.Import(HttpPostedFileBase UploadFile, FormCollection collection) in <removed this part of the path>\TableController.cs:line 392
The 10000 row file is 304KB in size, and I have the length limits set as follows in my web.config:
<httpRuntime
maxRequestLength="4096"
requestLengthDiskThreshold="1024"/>
I can't find anything special about 5 minutes, but it is always returning the error 5 minutes after I start the upload.
I have tried this with Chrome and IE. Both work via localhost, neither work via our dotnet app server.
The stack trace says it is blowing up on the SaveAs() method, but I can see the file on the network share location and the size matches.
After saving the file, I have to read it and return the data to a new view. There is a fair amount of processing being done on the data before returning it to the view, which is where I would expect to wait 5 minutes or more. After I'm done reading the file, I close the connection and delete the file from the network share. The file gets deleted shortly after I get the exception, too. The file does not exist when I start each upload.
I am the only person working with this application right now as it is still in development. I do not believe anyone else is accessing the saved copy on the network share while I am doing my testing. I can consistently reproduce this issue but I have no idea why it is happening.
Has anyone seen anything like this or have any suggestions for me? My guess is there's a setting somewhere on our app server but I haven't been able to pinpoint it.
Thanks!
Edit:
Here is the code that is handling the file upload.
// Validate, save, and read the selected file.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Import(HttpPostedFileBase UploadFile, FormCollection collection)
{
// Do some initial file validation - removed
string filePath = Path.Combine(EDTConstants.NETWORK_SHARE_PATH, Path.GetFileName(UploadFile.FileName));
try
{
// Do some validation on the file that was uploaded.
if (UploadFile == null)
{
// return an error here - there was no file selected.
}
if (UploadFile.ContentLength == 0)
{
// return an error here - the file is empty.
}
if (!filePath.ToUpper().EndsWith("XLS") && !filePath.ToUpper().EndsWith("XLSX"))
{
// return an error here - the file extension is not supported.
}
// Things are good so far. Save the file so we can read from it.
UploadFile.SaveAs(filePath);
DataSet fileDS = new DataSet();
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();
using (DataTable dtExcel = conn.GetSchema("Tables"))
{
// ... a bunch of code removed ...
}
conn.Close();
}
CloseFile(filePath);
}
catch (Exception e)
{
// Unexpected error
EDTUtils.LogErrorMessage(EDTConstants.TABLE_IMPORT, User.Identity.Name, e);
ViewData[EDTConstants.SSN_VD_ERROR_MSG] = EDTConstants.EM_UNEXPECTED + System.Environment.NewLine + e.Message;
ViewData[EDTConstants.VIEWDATA_FILE] = UploadFile.FileName;
CloseFile(filePath);
return View(tab);
}
// Closes a file - logs errors but does not throw any exceptions.
private void CloseFile(string filePath)
{
try
{
System.IO.File.Delete(filePath);
}
catch (IOException ioe)
{
EDTUtils.LogErrorMessage(EDTConstants.TABLE_IMPORT, User.Identity.Name, ioe);
}
}
I do see the same error logged, once when doing the SaveAs() and then again when doing the Delete. It's weird, though, the file does go away. It's almost like the app server has another thread trying to do something during this process, and that doesn't happen on localhost.
Edit
After I changed the executionTimeout value, I noticed that Chrome said "Uploading 79%..." at the 5 minute mark, then the file disappeared on the network share, then reappeared, and then after a minute or two, Chrome said the page was not available, with "Error code: ERR_INVALID_RESPONSE". The file has not been deleted from the network share yet. In IE, I am asked to log in to the app again at the 5 minute mark.
maxRequestLength is specified in KB but requestLengthDiskThreshold specifies the limit for the input stream buffering threshold, in bytes.
Therefore you need to change requestLengthDiskThreshold to 543744 (531KB x 1024)
Also check any of the following settings that apply to your scenario
<httpRuntime
executionTimeout = "HH:MM:SS"
maxRequestLength = "number"
requestLengthDiskThreshold = "number"
useFullyQualifiedRedirectUrl = "[True|False]"
minFreeThreads = "number"
minLocalRequestFreeThreads = "number"
appRequestQueueLimit = "number"
enableKernelOutputCache = "[True|False]"
enableVersionHeader = "[True|False]"
apartmentThreading = "[True|False]"
requireRootedSaveAsPath = "[True|False]"
enable = "[True|False]"
sendCacheControlHeader = "[True|False]"
shutdownTimeout = "HH:MM:SS"
delayNotificationTimeout = "HH:MM:SS"
waitChangeNotification = "number"
maxWaitChangeNotification = "number"
enableHeaderChecking = "[True|False]"
/>
Maybe a timeout is happening for the request, and the file is kept by the upload process and in the next attempt is still busy.
I suggest you delete any of the uploaded files, recycle you application pool, set a executionTimeout in the httpRuntime settings and try again.
The details of the httpRuntime settings are here
http://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.85%29.aspx
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Import(HttpPostedFileBase UploadFile, FormCollection collection)
{
// Do some initial file validation - removed
string filePath = Path.Combine(EDTConstants.NETWORK_SHARE_PATH, Path.GetFileName(UploadFile.FileName));
try
{
// Do some validation on the file that was uploaded.
if (UploadFile == null)
{
// return an error here - there was no file selected.
}
if (UploadFile.ContentLength == 0)
{
// return an error here - the file is empty.
}
if (!filePath.ToUpper().EndsWith("XLS") && !filePath.ToUpper().EndsWith("XLSX"))
{
// return an error here - the file extension is not supported.
}
// Things are good so far. Save the file so we can read from it.
UploadFile.SaveAs(filePath);
DataSet fileDS = new DataSet();
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();
using (DataTable dtExcel = conn.GetSchema("Tables"))
{
// ... a bunch of code removed ...
}
conn.Close();
}
}
catch (Exception e)
{
// Unexpected error
EDTUtils.LogErrorMessage(EDTConstants.TABLE_IMPORT, User.Identity.Name, e);
ViewData[EDTConstants.SSN_VD_ERROR_MSG] = EDTConstants.EM_UNEXPECTED + System.Environment.NewLine + e.Message;
ViewData[EDTConstants.VIEWDATA_FILE] = UploadFile.FileName;
}
finally
{
CloseFile(filePath);
}
//you were only returning in the CATCH ?
return View(tab);
}
Also check that there is no ANTIVIRUS trying to scan the files that you just uploaded

C# Access Database In use or Permission Failure

I'm using Access 2007 and C# to learn Databases. So far it's been rough but I've been able to handle things relatively well. What I need to do though is to query a my database table Accounts for the Amount of money a user has based on their pin. I've placed a button on the Windows Form I am using that will query the database on click. When I run/click the button as per normal I recieve the following error.
Essentially my question is this: How would I go about setting the permissions up so that my program can freely access the Access Database I have?
My Exception Error:
Exception: System.Data.OleDb.OleDbException: The Microsoft Office Access database engine cannot open or write to the file 'C:\Users\Public'. It is already opened exclusively by another user, or you need permission to view and write its data.
My code:
public partial class frmPin : Form
{
static string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public;Persist Security Info=True";
static private int pin = 11; //The First Pin in my records, for debugging I inserted it directly.
static string selectStatement = "SELECT Amount FROM Accounts WHERE(PIN=" + pin + ")";
OleDbConnection conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(selectStatement);
public frmPin()
{
InitializeComponent();
}
private void btnQry_Click(object sender, EventArgs e)
{
try
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader(); // executes query
while (reader.Read()) // if can read row from database
{
txtBx.Text = reader.GetValue(1).ToString();
}
}
catch (Exception ex)
{
txtBx.Text = "Exception: " + ex; // Displays Exception
}
finally
{
conn.Close(); // finally closes connection
}
}
"C:\Users\Public" needs to be changed to the actual path of the *.mdb file you want to access:
"C:\Users\Public.mdb"
OR
"C:\Users\Public\Something.mdb"
Depending on the name of your database:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
Or it may be an *.accdb file. Such as:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
See http://www.connectionstrings.com/access-2007 and http://www.connectionstrings.com/access
Also, sometimes you will get this kind of problem if you have the file open in another program like Access 2007, the file is marked as Read Only, or the security permissions are such that you don't have Read or Write Access. Note that if you set a "Deny" permission (in the filesystem/NTFS) for a group like Users, then it will override all other permissions, such that an Administrator would be effected by the Deny permission.
Edit: Thanks for comments, added a little clarification.

Categories

Resources