Invalid ConnectionString format for parameter "Version 3" - c#

I have created the object & also provided with the connection string (not sure whether it is accurate or not).
I am using ado.net 2.0/3.5 provider for sqlite in c#.
SQLiteTransaction trans;
SQLiteConnection con = new SQLiteConnection();
con.ConnectionString = #"Data Source = pathology.db ; Version 3";
con.Open();
trans = con.BeginTransaction();
int retval = 0;
After running above code I am getting invalid connection string format for parameter Version 3 on con.Open(); .
Need some guidance on it as I m a beginner using visual studio c# & sqlite.
Thank you!!

Should be Version=3;. Also you might need to specify full path to db file.
There is good site for connection strings: http://www.connectionstrings.com/sqlite/

Related

Local Database after publishing C#

i'm trying to make is so my program can have a local database after i have published it. Now i want it to work so i can install in on any computer so i have to change my connection string but i can't seem to figure out to what to change it to this is my connection string i have now
string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Barcode\\Application Files\\Barcode Scanning_1_0_0_0\\BarcodeDB.mdf\"; Integrated Security = True";
Before publishing i have went into applicationfiles and made sure that BarcodeDB.mdf is included but from what i've seen it changes to BarcodeDB.mdf.deploy after i've published it.
I've also went into Prerquisites and added
SQL Server 2012 Express LocalDB
When i try to run the published program or the debugger with the code i have now i get the error:
An attempt to attach an auto-named database for file C:\Barcode\Application Files\Barcode Scanning_1_0_0_0\BarcodeDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share
I'm guessing i need to use:
| DataDirectory |
But i'm new to all of this so i can't seem to figure it out how to use it even after searching for it so i would deeply appreciate it if someone could be kind to eiter explain how i chould be using DataDirectory or if i'm wrong and should be using something else.
And also sorry if i structured this question badly trying to get better at it
Best Regards Hannes.
Edit 1: Here is the code where i try to connect and using the database
string constring = $"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"{Application.ExecutablePath.ToString()}\\Application Files\\Barcode Scanning_1_0_0_0\\BarcodeDB.mdf\"; Integrated Security = True";
string Query = "SELECT Name FROM Products ORDER BY EDate;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString(myReader.GetOrdinal("Name"));
cbxProducts.Items.Add(sName);
cbxProducts.Sorted = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Ok I assume that your project is a Windows Forms or WPF project if it's not please tell me.
Your problem is that you are hard-coding a path to a database file which may not exist or your process can't have access to it's location. What you have found as | DataDirectory | is to use in web projects Web.Config file which will be mapped to App_Data folder.
In your case you have to build the connection string using your own applications executable path. Try this code:
//WPF:
string constring = $"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"{System.Reflection.Assembly.GetExecutingAssembly().Location}\\BarcodeDB.mdf\"; Integrated Security = True";
//Windows Forms:
string constring = $"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"{Application.ExecutablePath.ToString()}\\BarcodeDB.mdf\"; Integrated Security = True";
string constring = $"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=" + Directory.GetCurrentDirectory().ToString() + "\\BarcodeDB.mdf;Integrated Security=True";
This fixed it for me

Format of the initialization string does not conform to the OLE DB specification

I'm getting the following error when trying to read an Excel file using a third-party component
Format of the initialization string does not conform to the OLE DB specification
Now, I know the words "third party component" are going to set off alarm bells here, but hear me out.
Here's the connection string I'm using
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\users\rory\downloads\testdb_4.xls;
Extended Properties='Excel 8.0;HDR=YES;';
I've got this exact connection string to work no problem with the following C# code
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
conn.ConnectionString = connstring;
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "select * from [TEST_DB$]";
// TEST_DB is the name of the sheet
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
// do stuff with dt
}
}
}
This works as expected, and the datatable is filled with data from the Excel file. However when I try to access it from the component, I get the error above.
I'm not an expert on OLE DB, but I was under the impression that the drivers are at the OS level and if they work for one app / connection string they should work for all apps with that same connection string. Am I wrong? And if so, does anyone have a clue what's going on here?
I have contacted support for the component.
The solution to this ended up being to replace the single quotes with escaped doubles.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\users\rory\downloads\testdb_4.xls;" +
"Extended Properties='Excel 8.0;HDR=YES;'"; // single quotes
became
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\users\rory\downloads\testdb_4.xls;" +
"Extended Properties=\"Excel 8.0;HDR=YES;\""; // escaped double quotes
OleDbConnection had no problem with single quotes but apparently some apps and components do.

how to create connection string for oracle c#

i am creating connection string for oracle in c#
below is my code
OracleConnection cn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.5 )(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));User Id=nTireoffice ;Password=nTireoffice;");
DataTable dt = new DataTable();
string strSql = " select to_number (nvl(max(nvl(Region_ID ,0)),0)+1 ) as No from BO_REGION_MASTER ;";
cn.Open();
OracleDataAdapter objSda = new OracleDataAdapter(strSql, cn);
objSda.Fill(dt);
string s = dt.Rows[0][0].ToString();
its throwing exception ORA-06413: Connection not open.
i am using windows 8 enterprises 64 bit os
thank u...
Have a look here. (This is a very useful site for clues about most ORA errors.)
It sounds like it could be the folder your executable is in, or the actual name of your executable.
PS It would be useful if you could tell us the actual line the error occurs on.
I've created the connection like this.
ora_cmd = new OracleCommand();
ora_con = new OracleConnection("Data Source={YOUR_HOST}:{YOUR_PORT}/{YOUR_DB};Persist Security Info=True;User ID={YOUR_ID};Password={YOUR_PASSWORD};Pooling=True;Max Pool Size=200;");
ora_cmd.Connection = ora_con;
ora_connect.Open();
Then use the ora_cmd to executes statements

OleDB connection string for SQL Server in a C# program

I have seen lots of answers to connect to MS Access via OleDB but there is not good answer for SQL Server. I try to connect to a SQL Server database via OleDB provider in my C# program.
This is the connection string I am providing.
Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI
But it gives me error
‘Keyword not support ‘Provider’’
What I want to do here is connect database via OleDB in C# program.
This works as expected on my side. From the error message I strongly suspect that you are using the SqlConnection class instead of the OleDbConnection (Of course you need to use all the other classes provided by OleDb like OleDbCommand, OleDbDataReader etc...)
string connStr = "Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI";
using(OleDbConnection cnn = new OleDbConnection(connStr))
{
....
}
When in doubt, use the string builder in visual studio. That way unsupported keywords can't creep into your connection strings, the following is a wonderful example on how to use it.
http://www.c-sharpcorner.com/uploadfile/suthish_nair/how-to-generate-or-find-connection-string-from-visual-studio/
The same Connection string is working fine at my end.
I am posting my sample code which is executes successfully at my end
public string connStr = "Provider=SQLOLEDB;Data Source=.;Initial Catalog=<dbName>;Integrated Security=SSPI";
public OleDbConnection con;
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
public void Test()
{
con = new OleDbConnection(connStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from tblApartments", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
}
Please place breakpoint and check line to line and when your breakpoint comes to con.close(); then check ds, you can see the output.
The connection string you're using, considering the OLE DB provider, is correct. I didn't find any error in the connection string used, if you want to connect to a SQL Server data source.
Most probably, the reason of that error should be that you're not using correctly all the classes and objects required by the OLE DB provider, like OleDbCommand (that is similar to a SqlCommand but it's different), OleDbConnection, OleDbDataAdapter and so on. In a nutshell, the reason of that error should be this:
string connStr = "Provider=SQLOLEDB;Data Source=<servername>;Initial Catalog=<dbname>;Integrated Security=SSPI";
using(SqlConnection scn = new SqlConnection(connStr))
{
....
}
Indeed, using a SqlConnection object, the ConnectionString property doesn't support the keyword Provider and, executing your application, you got an error about a keyword not supported.
Have a look at this simple tutorial about the use of OLE DB provider.

.Net C# how to connect to external SQL Server database ? OleDb or other?

Hi I would like to know how I should connect to the external SQL Server database in C# , .NET ?
For example if I have there parameters :
SQL info
Url to get to database (throughout browser also): Sqlweb.companyname.com
Database username: username
Server: Dcms-xxx
Databasename: databaseName
Databasepassword: password
?
I know how to connect to internal : Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory + "..\\Files\\MapPlaces\\Database.mdb;";
But what about external ?
I have tried :
string nowConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sqlweb.companyname.com;Initial Catalog = databaseName; User Id = Username; Password = Password;";
System.Data.OleDb.OleDbConnection dbcon = new System.Data.OleDb.OleDbConnection(nowConString);
string sql = "SELECT * FROM XXXTable";
dbcon.Open();
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, dbcon);
System.Data.OleDb.OleDbDataReader reader;
reader = cmd.ExecuteReader();
ScriptStuff.Append("Reader created!<br/>");
while (reader.Read())
{
string companyName = reader.GetValue(1).ToString();
ScriptStuff.Append(companyName+"<br/>");
}
Did not work ! Thank you for your help !
Edited from comments:
Yes that was one my mistake, thanks. Since first one was access and YES second is SQL Server. And it is SQL Server 2005. But I am new to .net and all that... I have found first one and second one in that connectionstring.com but I could not find or understand how to use that for this one ...
Could you help, and just post hole connection ? Thanks – Vilius 7 mins ago
I mean do I still need to use OleDB ? should there be "Provider=Microsoft.Jet.OLEDB.4.0;" in that connection string ? Where do i post what (server (that Dcms-xxx), or url of the sql server (sqlweb.companyname.com))? THANKS FOR YOUR HELP ! –
I would add a connectionString to my app/web.config.
<connectionStrings>
<add name="AspnetdbConnectionString"
connectionString="Data Source=<databaseadress>;Initial Catalog=<database>;User Id=<user>;Password=password>"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
The above example is how you specify an connectionstring for a MSSQL connection, and below a way to use this connectionstring.
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AspnetdbconnectionString"].ConnectionString))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.Text;
cm.CommandText = "SELECT * FROM ...";
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
while (dr.Read())
{
// do stuff
}
}
}
}
Are you sure that it's a SQL Server database that you are trying to connect to?
Your "internal" example connects to a Microsoft Access database (OLEDB provider and database file extension .mdb)
If your external database is really a SQL Server database, the recommended way is using SqlConnection, SqlDataReader and so on instead of OleDbConnection etc.
Or, if you really want to use OleDb, you need a different connection string.
See connectionstrings.com (for SQL Server 2008, 2005 or 2000, depending what you're trying to connect to).
I would highly recommend taking a look at:
http://www.connectionstrings.com/
It's a quick, "in you face" treatment of the subject of connection strings for all major databases.

Categories