Using Npgsql provider with OLE database connection - c#

So I have the following connection string which is causing me issues. I am attempting to write an application which is capable of being database agnostic (difficult but not impossible) and I have come to testing it on PostgreSQL however I cannot seem to get an OLE connection to work with it. From what I have seen it is entirely possible.
I got everything working using a specific class for PostgreSQL but this used the Npgsql types rather than generic OLE types.
E.g.
private NpgsqlCommand m_postgreSQLDatabaseCommand;
Instead of
private OleDbCommand m_oleDatabaseCommand;
This is not acceptable in my circumstances.
I tried simply changing the connection string to use the Npgsql provider but I am not sure if I am using it correctly. I get the error below:
Connection String:
<add name="ApplicationPostgresDefault"
connectionString="Server=127.0.0.1;Port=5432;
Database=myDatabase;
User Id=myUser;
Password=myPass;
Provider=Npgsql"
/>
Error:
The 'Npgsql' provider is not registered on the local machine.
What can be done to solve this?
If I get the provider in there correctly will I be able to communicate with PostgreSQL as I was with Npgsql objects?

The local machine doesn't have the provider installed.
I would suggest using a native client rather than OleDb anyway:
http://npgsql.projects.postgresql.org/
http://connectionstrings.com/Providers/npgsql
that's your choose

Are you missing a ; after Npgsql ?

Related

ASP.NET MVC C# change code from accessing a mySQL database to a MSSQL database

I have an application that I used to connect to a mySQL database. I have installed the MySQL.Data package and I use this code to build my connection:
services.AddScoped<System.Data.IDbConnection>((s) =>
{
IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("databasename"));
conn.Open();
return conn;
});
This works fine as I am able to read/write data from the database.
Now, I need to have the same application access a MS SQL database. The tables are all the same and the fields on each table are the same as well. I am having a hard time finding what I should change my code to (or what package I should include). When I remove the MySQL.Data package, the MySqlConnection function becomes invalid (as would be expected).
Any idea what package to include and what function to use to establish the connection?
Any assistance is greatly appreciated.
Thank you!
EDIT #1
I found an old stackoverflow post (I should have referenced it here but I closed the window) that talks about this very issue. The suggestion was to add
using System.Data.SqlClient;
and then change the assignment code to
IDbConnection conn = new SqlConnection(Configuration.GetConnectionString("databasename"));
I made these changes and now I get this error within the code:
Im at a loss as to how to resolve this. I verified that my project is in framework .NET core 3.1
Not sure about size of your project and detailed requirements but I would suggest to use libraries like:
Entity Framework - no SQL knowledge needed, can connect to any Db technology, almost no code changes required when switching between Db providers
or
Dapper - fast lightweight also supports multiple Db providers but you need to write correct SQL commands yourself specific for each Db technology you use

"DB2OLEDB provider is not registered on the local machine" exception in C# code...accessing DB2 database from C#

I am attempting to connect to DB2 database from C#.Net. The connection string i used is:
Provider=DB2OLEDB;Cache Authentication=True;Password=xxxxxxx;Persist Security Info=True;User ID=xxxxxx;Initial Catalog=xxxxxxxx;Data Source=xxxxxxxxx;Defer Prepare=False;Derive Parameters=False;Network Transport Library=TCPIP;Host CCSID=37;PC Code Page=1252;Network Address=xxxxxxxxx;Network Port=446;Package Collection=CDATA;Default Schema=CDATA;DBMS Platform=DB2/AS400;Process Binary as Character=False;Connection Pooling=True;Units of Work=RUW
On executing the ExecuteReader(), i keep getting the runtime exception:
The 'DB2OLEDB' provider isn't registered on the local system.
I have already included the reference to library "System.Data.OleDb" in my class.
The DB2 resides on a different server and i don't have any db2 instance on my local machine. How do I solve this difficulty?
Can someone please help me with fixing this error, is it any other dll reference that I am missing? I am assuming I do not need to get any DB2 client installaed on my machine in order to be fixing this issue, could it be done by just a dll reference?
Change the provider name from "DB2OLEDB" to "IBMDADB2" and see also here: IBM Knowledge Center

Accessing oracle database from c# with odb.net

I'm trying to access Oracle 11g DB.
According to connectionstring.com , the connstring should be :
Driver={Oracle in OraClient11g_home1};Dbq=myTNSServiceName;Uid=user;Pwd=passwd;
Anyhow I'm getting an ArgumentException: 'Driver' is not a valid word.
Thank you.
Greetings
R. Bada
It looks like you are trying to use connection string which is suitable in case of ODBC drivers for Oracle.
You should look for connection string suitable for ODP.NET in Oracle Data Provider for .NET / ODP.NET section. The best is to use one of the following:
Data Source=TNSServiceName;User Id=user;Password=passwd;
or
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=user;Password=passwd;
The first is suitable when you are using TNS, the second allows you to provide the service definition in connection string.

Create connection to database in VS2010 without SQL Client - dynamically

How to create connection to database in VS2010 without SQL Client dynamically?
I tried using ConnectionStringSettings and SqlConnection and I succeeded, but I'm not supposed to use SQL Client.
I'm using Massive.
You should be able to use System.Data.OleDb.OleDbConnection.
Use a connection string like this:
Provider=SQLOLEDB;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;
It might be that you just miss to reference System.Configuration in your project and consequently Massive cannot find the connection string in your app.config.
Following the instructions on github I could query my SQL Express database without any problems using Massive having a plain SqlClient connection string in my app.config.

Is there a generic way of dealing with varying connection strings in C#?

I have an application that needs to connect to a SQL database, and execute a SQL Agent Job.
The connection string I am trying to access is stored in the registry, which is easily enough pulled out.
This appliction is to be run on multiple computers, and I cannot guarantee the format of this connection string being consistent across these computers. Two that I have pulled out for example are:
Data Source=Server1;Initial Catalog=DB1;Integrated Security=SSPI;
Data Source=Server2;Initial Catalog=DB1;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;
I can use an object of type System.Data.SqlClient.SqlConnection to connect to the database with the first connection string, howevever, I get the following error when I pass the second to it:
keyword not supported: 'provider'
Similarly, I can use the an object of type System.Data.OleDb.OleDbConnection to connect to the database with the second connection string, howevever, I get the following error when I pass the first to it:
An OLEDB Provider was not specified in the ConnectionString'
I can solve this by scanning the string for 'Provider' and doing the connect conditionally, however I can't help but feel that there is a better way of doing this, and handle the connection strings in a more generic fashion.
Does anyone have any suggestions?
The normal way of handling this is storing the ADO.NET provider unique name (separately from the connection string) and using a DB Provider Factory.
Use a SqlConnectionStringBuilder, initialize it with the connection string you find in registry and then read its ConnectionString property, which would normalize the connection string to proper SQL Client syntax.

Categories

Resources