I am creating an application which uses access file from shared network.The application works fine when the access file is placed local.But when I placed it in shared path and trying to connect.It is throwing an exception."An OLE DB Provider was not specified in the ConnectionString. 'Provider=SQLOLEDB
Here is my code:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\server\\MyFirstProject\\SampleDB2.accdb";
using (OleDbConnection con = new OleDbConnection(connectionString))
{
string command = "INSERT INTO emp_status(emp_id,hours,feeded_on,comments)";
command += "VALUES(#emp_id,#hours,#feeded_on,#comments)";
Could anyone help me how to resolve this issue and Am I missing something?
I am using OLEB as a datasourse and Access 2013
Any Suggestions or explanations would definitely helps me.
Thanks in advance!!
You need to escape the leading double slash as well...
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\\\server\\MyFirstProject\\SampleDB2.accdb";
I haven't got anything to check this on here, but I'm fairly sure this is your answer. Also, watch out for reserved words, E.g. "hours" might be one in which case it should be [hours].
Also, you haven't specifically included adding the parameter values in your question. I assume you are doing it but if not then see my coding example here...
VB 2010 error (INSERT INTO) syntax error
Related
Authentication to host '127.0.0.1' for user 'root' using method 'mysql_native_password' failed with message: Reading from the stream has failed.
I've searched a lot about this issue and only Internet disconnect solved the problem. Why is that?
I use MYSQL 8 and C# winforms.
Connection code looks like this:
MySqlConnection conn = new MySqlConnection("datasource=127.0.0.1;username=root;password=password;database=my_tests");
The every next try I execute the command is successful! It happens only first time.
I very appreciate your time people. And would be very thankful for constructive help for a newbie like me.
When I put SslMode=None to a string so it looks like
MySqlConnection conn = new MySqlConnection("datasource=127.0.0.1;username=root;password=password;database=my_tests;SslMode=None");
the problem is gone. But I'm not sure if I want not to use it. Any other solutions would be great.
try this code you can use the server ip address instead of local
string connectionString = "server=(local)\SQLExpress;database=my_tests;password:qwerty;integrated Security=SSPI;";
SqlConnection _con = new SqlConnection(connectionString)
I can't Insert and select from Local database data in C#.
I've read these articles
C# - Writing data in local database
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=vs.100).aspx
How to add local database items into textBox using listBox in C#
All the code samples are the same, here's my sample.
SqlCeConnection conn = new SqlCeConnection(#"Data Source=|DataDirectory|\PacjenciDB.sdf");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO pacjenci (nazwiskoimie,adres,skierowany,opis) values (#nazwiskoimie,#adres,#skierowany,#opis)";
cmd.Parameters.AddWithValue("#nazwiskoimie", txtnazwiskoimie.Text);
cmd.Parameters.AddWithValue("#adres", txtadres.Text);
cmd.Parameters.AddWithValue("#skierowany", txtskierowany.Text);
cmd.Parameters.AddWithValue("#opis", txtopis.Text);
cmd.ExecuteNonQuery();
conn.Close();
Can someone tell me what I'm doing wrong?
I've tried tons of samples about insert data, but it doesn't work.
I can manage .MDF, but .SDF seems quite problematic.
Hope you help me
Ok, I'm going to take a guess here. Is the PacjenciDB.sdf included into Visual Studio project by any chance? Do you have the property "Copy to output folder" set to "Always" or something similar? It seems that every time you do a build you could be overwriting your output folder database file. Try putting the database in a folder that is not inside VS project.
BTW, your code is OK.
Look for a copy of the database with data in your bin/debug folder.
Solution is to not use |DataDirectory|, but use full path instead.
The above code is correct and most likely the problem is somewhere else, where you call that code from. If there is a problem with db connection or data is wrong - you should get an exception. Since there is no error occurred and no new records added - the code is not executed at all.
P.S.
.sdf is a Sql Server Compact Local Database, so using System.Data.SqlServerCe is correct
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce(v=vs.100).aspx
Try using double backslash when setting the path to your database file:
string dbPath + "Data Source=C:\\DataDirectory\\PacjenciDB.sdf";
SqlCeConnection conn = new SqlCeConnection(dbPath);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO pacjenci (nazwiskoimie,adres,skierowany,opis) values (#nazwiskoimie,#adres,#skierowany,#opis)";
cmd.Parameters.AddWithValue("#nazwiskoimie", txtnazwiskoimie.Text);
cmd.Parameters.AddWithValue("#adres", txtadres.Text);
cmd.Parameters.AddWithValue("#skierowany", txtskierowany.Text);
cmd.Parameters.AddWithValue("#opis", txtopis.Text);
cmd.ExecuteNonQuery();
conn.Close();
I have a folder 'Data' in my WPF application in which there is an .sdf database file.
This file is the database for my application.
When developing my app I used a fixed path to my db like this:
'Data Source=P:\Dropbox\Projects\MembersApp\MembersApp\bin\Debug\Data\RF_db.sdf'
Now I want to use the |DataDirectory| value so that the app always can find the db, were ever the app is installed. I found this solution on StackOverflow:
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
string dataSourceHome = "Data Source=|DataDirectory|\RF_db.sdf";
But is giving me an error on the last line 'Bad compile constant value'. I've tried with:
string dataSourceHome = #"Data Source=|DataDirectory|\RF_db.sdf";
But that doesn't work.
Any idea what's wrong here?
Do not change DataDirectory in your code; it is set by the installer and changing it will prevent your app from knowing where the data was installed. Just use:
string dataSourceHome = #"Data Source=|DataDirectory|\RF_db.sdf";
And nothing else. Do not call AppDomain.CurrentDomain.SetData("DataDirectory", path); that's what is breaking things.
You could use:
string dataSourceHome = string.Format("Data Source={0}\\RF_db.sdf", Environment.CurrentDirectory);
or
string dataSourceHome = string.Format("Data Source={0}\\RF_db.sdf", System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
It looks like your creating a ADO.Net connection string. Isn't there just information missing from your dataSourceHome?
Have a look at this post.
Or a different approach to creating the connection might be found on ConnectionStrings.com.
I have a Sqlite database that I include with my MonoTouch app. It has worked fine for me so far, but now I want to open it in read-only mode rather than read-write.
So I have changed the connection string to include 'Read Only=True', but when I call Open(), I get the following error:
Library used incorrectly (at Mono.Data.Sqlite3.Open)
If I dig into the exception it shows
_errorCode = Misuse
and that's about all the info it gives.
Here's the code:
var _conn = new SqliteConnection("Data Source=db/sampleDb;Read Only=True");
_conn.Open ();
You found a bug in Mono.Data.Sqlite.dll.
The Create flag is appended (by default) before the ReadOnly flag is parsed and set. The resulting flag is invalid and sqlite reports an error.
I will fix this for future releases (of Mono and MonoTouch...). If this blocks you then please open a bug report on http://bugzilla.xamarin.com and I'll attach a fixed assembly (with instructions to replace the existing one) to the bug report.
This worked for me (aspnet core):
var _conn = new SqliteConnection("Data Source=db/sampleDb;mode=ReadOnly");
Have you tried?:
var _conn = new SqliteConnection("Data Source=db/sampleDb;mode=ro");
Your code is correct, I just tried it (not using MonoTouch) and it worked for me.
Do you have the latest version of System.Data.SQLite.dll?
If yes, then maybe it's a problem related to MonoTouch.
I know it was 1000000000 times already, but none solution helped to me.
I want to insert data in C# using OleDB. I tried mln solutions but here is the easiest one which should work but it doesn't:
SQLCONNECTION = #"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\dbScenariusz.mdb";
using (OleDbConnection connection = new OleDbConnection(SQLCONNECTION))
{
string sql = "INSERT INTO Table (content) VALUES('lala')";
connection.Open();
OleDbCommand command = new OleDbCommand(sql, connection);
command.ExecuteNonQuery();
connection.Close();
}
SQLCONNECTION is ok. It works fine for the SELECT query.
string sql - I tried this query in Access and it works fine.
I get no error. It just didn't insert anything to the database.
When I run the query in Access (the same database) the row is inserted.
The strange thing is that command.ExecuteNonQuery(); returns 1! That means that 1 row was affected!
I really have no idea where the problem is, so I really appreciate any help.
Sorry for my english.
UPDATE: Another strange thing. I change query to update and it works fine! really wtf? :)
You are connecting to the DataDirectory. Is the .mbd file being copied after the build? In other words are you re-deploying the database with each build and thereby losing the inserts?
A quick test could be using the same code and same connection to do a select after the insert.
In the solution Explorer, check app.config. Double click the app.config, then re-enter the connectionString. Give the path of your database.
For example, in my project the default location of connectionString is:
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\database.accdb;Persist Security Info=True"
Suppose the database is stored in this location:
C:\Documents and Settings\Amuk\My Documents\Visual Studio 2010\Projects\insertion\insertion\App_Data
therefore, replace the connectionString with
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Amuk\My Documents\Visual Studio 2010\Projects\insertion\insertion\App_Data\database.accdb;Persist Security Info=True"
your problem will be solved, i hope this will definitely help you, i was also getting the same problem, and by replacing the connectionString with original path, the database is storing all records.
You can try to use transaction to commit your changes.
command.Transaction = connection.BeginTransaction(); //after connection.open()
Then add
command.Transaction.Commit(); //Before connection.close()