Unable to open shared SQLite database - vb.net

Unable to open database file
That's the error I get when trying to open a SQLite db file that is shared, I've already allowed read/write to everyone when I shared the folder containing the db file.
I'm accessing the file using LINQPad or SQL Server Compact & SQLite Toolbox both gives me the same error.
However I can successfully work with the database using my Windows Form application with the following declaration for the connection
Dim litecon As SQLiteConnection = New SQLiteConnection("Data Source=\\10.10.10.10\SQLite\SQLiteDB.db;Version=3;Password=1;", True)
I set parseViaFramework to True

Related

Error while uploading Excel data to SQL server 2005

I have a requirement to upload excel data into a SQL Server 2005 table. Initially I copied Excel data to a temp table and based on condition I have updated and inserted records into the SQL Server table.
Everything works fine in my local system. Same program I moved to quality server there I faced this error
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Then we installed 'Microsoft.ACE.OLEDB.12.0 provider on quality server. Now this error appearing
The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data."
Kindly help me out to solve this issue.
SqlConnection con = new SqlConnection();
con = SQLManager.openSQLConnection();
string path = FileUpload1.PostedFile.FileName;
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [CM_CODE],[CM_NAME],[CM_ADD1],[CM_ADD2],[CM_ADD3],[CM_ADD4],[CM_CITY],[CM_PHONE],[CM_CG_CODE],[CM_CO_CODE],[CM_EXPIRY_DATE],[CM_PINCODE],[CM_EMAIL] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
Regards,
Sathya
This is most likely a file permissions issue. Does the account that the program runs under have folder level permission to read files from the file location.
Find out which account the program runs under and then add appropriate access permissions to the folder where you upload your excel files.
If it's IIS then whichever account your app pool runs under needs write access to the folder that you upload to. Permissions can usually only be set by web server admin.
Wing
Finally i found out the solution.
I have created one folder under my application folder in Quality server.
Uploaded excel sheets wil get stored under this new folder.
and that path i am referring while updating my SQL server database.
Everything works fine now...
Thank u all for your support
Regards,
Sathya

Making a back up of A SQL Server Compact Edition Corrupts the database

On close of an application written in VB, I need to copy the .sdf file, this is the local SQL Server CE database that syncs with the server. Each time we close the app we require a backup to be made.
I am using the file.copy method. First I close the connection to the database then copy the file to a specific location.
Dim conn As SqlCeConnection = New SqlCeConnection(My.Settings.MyClientConnectionString)
conn.Open()
conn.Close()
Dim tpath As String = My.Settings.CertPath
Dim path As String = tpath.Replace("db\", "MyDatabase.sdf")
Dim fs As FileStream = File.Create(path)
fs.Close()
File.Delete("C:\db\backup\MyDatabase.sdf")
File.Copy(path, "C:\db\backup\MyDatabase.sdf")
The problem arises on the line of code:
Dim fs As FileStream = File.Create(path)
The database becomes corrupted and has a size of 0 kb.
Has anyone seen this before? Thanks
Are you calling Create on an existing database file? According to the docs, Create creates or overwrites a file in the specified path. In the code you show, calling Create isn't necessary anyway. Just call File.Copy.

What is proper way to define connection string VB

My Visual Basic project uses this connecton string:
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Desktop\IzolacCold V2\IzolacCold V2\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
When I debug it, and go to debug folder, or when I "Release" it then problem occurs. If I run exe file program works normally but it save's data in database defined in connection string and not in database that is created in DEBUG folder (or RELESE folder).
How to properly connect to my databes. How to build proper connection string ?
You should use the DataDirectory substitution string.
For example you could write your connectionstring as
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
In your code then you could control the exact location of the DataDirectory executing this code, BEFORE any attempt to open a connection.
Dim commonDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim myAppDataFolder = Path.Combine(commonDataFolder, "MyAppName")
if Not Directory.Exists(myAppDataFolder) Directory.CreateDirectory(myAppDataFolder)
AppDomain.CurrentDomain.SetData("DataDirectory", myAppDataFolder)
(This example use the C:\programdata\myAppName folder because it is the most appropriate location for read/write data files needed by your application
Your deployment procedure should take care to create the above folder and place the database file in this location.

I want my vb.net program with sqldb to be transferable

I'm having trouble with my vb.net program. It has a database with a SqlConnection string of:
DbConn = New SqlConnection("Data Source=ACE-DUO;Initial Catalog=db_CVSO;Persist Security Info=True;User ID=sa;Password=pwd")
I made an installer for this vb.net program but I'm having problems regarding my SQL Server connection string. It's because once I installed the program in different computer. The server name in my case (ACE-DUO) changes and the database itself cannot be located.
I know how to detach the file and attach it to vb.net program. what I'm really aiming for is that I want the connection string to change on based where the program resources were placed.
For example, if the program was installed in the C:\Program Files\MyDatabase folder, I want to make it as a part of the connection string so it would be opened in different computer.
If you don't need multiple shared access to your database you could take advandage of the LocalDB feature of Sql Server 2012.
You connection string could be changed to
DbConn = New SqlConnection("Server=(localdb)\v11.0;Integrated " & _
"Security=true;AttachDbFileName=C:\Program Files\MyDatabase\db.mdf;"
Article on LocalDB

Access remote sql server using VB.NET

I've found a few examples of using vb.net to access an sql database, so far none of them have worked . They all involve using DataReaders. Maybe its the fact that the sql db is not on the same machine as the application.
I was just wondering if anyone had a more comprehensive example of using VB.NET to access a remote sql server.
Thanks!
EDIT:
I've received a few helpful comments an replies already. So far my connection string looks like:
"server=sqlblah.myhost.com;uid=myuser;pwd=pass;database=testdb"
Probably also good to mention their is no editing of the tables a this point, just reading.
Check out the SQLClient class.
One convienent way to access a SQL database is to fill a DataSet object with query data with a DataAdapter object.
Dim sSQL As String = "SELECT * FROM ???"
Dim conn As New SqlClient.SqlConnection("connection string")
Dim da As New SqlClient.DataAdapter(sSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "TABLE NAME")
You can then access the "TABLE NAME" table in the DataSet object.
The "connection string" is obviously your SQL connection string.
Use the sSQL string to query as necessary.
Quick side note - a helpful tool for creating connection strings:-
Open up a text document (notepad, wordpad etc) and save a blank document with the extension ".UDL".
This will give you a "Data link Properties" mini app.
Open up the app and change the provider in the "Provider" to whichever provider you need (in this case OLE DB Provider for SQL Server).
You then need to build up the connection in the connection tab.
Once you have chosen the criteria (ServerName (the drop down list will show you all visible Servers), Security permissions,Database (this drop down list will be populated based on the server chosen)) you can test your connection (to make sure you have permissions etc).
Click ok to close the App, rename the file to have a ".txt" extension and re-open in a text editor, hey presto, one built up connection string (as below).
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YOURDBNAME;Data Source=YOURSERVERNAME