Query PostgreSQL db in C# in database agnostic way (ie without Npgsql) - c#

Is there a way to query a PostgreSQL db in C# in a database agnostic way ie without Npgsql. I want use built in .NET libraries and a connection-string that contains the DB IP, Username, Password and any other info that the connection-string might require.

You can use ODBC and construct the connection within your app. The Postgres driver has to be installed on the machine running the program. Your app would then only be using the generic ODBC classes. Below is a simple example.
using System.Data.Odbc;
string connectString = "Driver={PostgreSQL UNICODE};Port=5432;Server=localhost;Database=myDBname;Uid=myusername;Pwd=mypassword;";
OdbcConnection connection = new OdbcConnection(connectString);
connection.Open();
string sql = "select version()";
OdbcCommand cmd = new OdbcCommand(sql, connection);
OdbcDataReader dr = cmd.ExecuteReader();
dr.Read();
string dbVersion = dr.GetString(0);

Related

Can anyone suggest a simple way to update an SQLite database with changed data using Microsoft.Data.Sqlite which has no DataAdapter?

I am converting a C# project from using an SQL Server Compact database to SQLite using Microsoft.Data.Sqlite.
My main problem is that there is no equivalent of SqlCeDataAdapter (i.e. no SqliteDataAdapter) in Microsoft.Data.Sqlite.
Can anyone suggest a simple way to update an SQLite database with changed data from a DataSet (the equivalent of DataAdapter.Update())?
I would prefer to use Microsoft.Data.Sqlite rather than System.Data.SQLite (which has an SqliteDataAdapter).
You can write sql update queries for an SQLite database without the use of an adapter by using the SQLiteCommand method:
string cs = "Data Source=:memory:";
string stm = "SELECT * FROM table";
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(stm, con);
string version = cmd.ExecuteScalar().ToString();
Source: https://zetcode.com/csharp/sqlite/

How to access the localhost connection to MySQL database through c#?

In my C# code, I have this in my App.config
<add name="SampleDB" connectionString="Server=.;Database=Sample;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
I opened MySQL Command Client, typed in the password, then created the Sample database by typing CREATE DATABASE SAMPLE;
However, I cannot connect to the database. Do I need to specify the instance of SQL after "Server=.?
Or do I need to open the connection some other way?
Bookmark the ConnectionStrings.com. This site provides all types of connectionstring information for all types of databases and versions of drivers, including MySQL.
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Put a piece of code here starting from the declaration of the MySqlConnection object so we can better understand why it is not connecting because the connection string answer above is already correct. If you installed MySQL using default settings, it should be:
<appSettings>
<add key="MyConnectionSettings" value="Server=127.0.0.1;Port=3306;Database=myDataBase;Uid=root or user;
Pwd=yourpassword" />
string connectionFromConfig = ConfigurationManager.AppSettings["MyConnectionSettings"];
using(MySqlConnection con = new MySqlConnection(connectionFromConfig)){
con.Open();
string sql = "SELECT *from yourtable";
MySqlCommand cmd = new MySqlCommand(sql, con );
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]+" -- "+rdr[1]);
}
rdr.Close();
}

Using Oledb connection string for Localhost c#

When use Oledb c# connection
I noticed that a lot connection using file.
But how to connect to localhost using oledb?
I created database and tables using Microsoft SQL Server Management that connect with SQL Express and using window authentication
When using this function i don't know how should convert to connect to localhost
//Want the connString to connect localhost instead of file
public static string connString = #"Provider=Microsoft.JET.OLEDB.4.0;data source=" + Path + "\\database\\errDB.mdb";
public static OleDbConnection connection;
public myFunction()
{
string sqlString = "SELECT name,contact,accessLevel,Crudential_ID FROM errors where Crudential_ID =#ID";
connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand(sqlString, connection);
//Open connection
connection.Open();
command.Parameters.Add("#ID", OleDbType.VarChar);
command.Parameters["#ID"].Value = "test";
//Read from database
OleDbDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
.....
}
connection.Close();
}
connectionstrings.com - true to its name - is indispensable when you frequently need to construct connection strings. For your specific case, this would be the relevant section.
Based on that, your connection string should look something like this:
Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=SOMEDATABASE;Trusted_Connection=yes;
To break it down:
SQLNCLI11 is the SQL Native Client OLEDB provider. You can see available providers in SQL Management Studio, under Server Objects > Linked Servers > Providers.
.\SQLEXPRESS is your servername and instance. The . is shorthand for localhost (you can also use localhost if you prefer), and SQLEXPRESS is the default instance name that SQL Express installs under.
SOMEDATABASE - whatever your database name is.
Trusted_Connection=yes - Use windows authentication. Sometime you see it as Integrated Security=SSPI. They are one and the same.
If you are using SQL Express then I would suggest using a System.Data.SqlClient.SqlConnection object to make your connection. You will only need your server name to connect.
Server=ServerName\SQLEXPRESS;Database=Blah;User ID=user;Password=pw

Connect to SQL Server with ODBC alias

I have an MSDE installed and I have a DB for it. And on a client computer an ODBC alias (x). I want to connect to this using a .NET4 program written in C#. What connection string should I use if I have only a login name (y) and a password (z)?
Or am I to extract server and database name from the registry?
Try this:
Provider=MSDASQL.1;Data Source=x
where x is your ODBC alias. You will need to add security information to this, as per normal.
http://support.microsoft.com/kb/310988
The example #4 is for the DSN use.
{
OdbcConnection cn;
OdbcCommand cmd;
string MyString;
MyString="Select * from Customers";
cn= new OdbcConnection("dsn=myDSN;UID=myUid;PWD=myPwd;");
cmd=new OdbcCommand(MyString,cn);
cn.Open();
MessageBox.Show("Connected");
cn.Close();
}

MissingManifestResourceException through SmartDeviceProject?

I am new to SmartDeviceProject. So When i try to connect MySQL to my project, MissingManifestResourceException through in Connection.open in below code?.
string connectionString = "server=192.168.1.100;database=mcubic;User Name=mcubic;Password=mcs#2011$;";
string query = "select b.Outlet_Master_Name from mcs_user_outlet a,outlet_master b where a.Mcs_User_Outlet_User_Id=3 and a.Mcs_User_Outlet_Outlet_Id = b.Outlet_Master_Id";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
comboBox1.Items.Add(Reader[0].ToString());
}
connection.Close();
How do i solve this,. I am imported MySql.Data.CF.dll for this project.
Via Krish Kapadia from MSDN forums:
Solution :
MySql's version 5.2.7.0 is the stable version. I use dll of this version. Other versions have many problems. so first I download dll of that version from mysql site.
here is the link :
http://dev.mysql.com/downloads/connector/net/5.2.html
I have inserted one entry in 'mysql.user' table in which hostname will be '%' (means any user can connect to mysql). If you don't want to insert entry with hostname then you have to insert for all ip addresses who should be allowed to connect to mysql.
After inserting entry in 'mysql.user', I restarted MySQL service.
And then trying to connect to mysql and Connected....
source:
http://social.msdn.microsoft.com/Forums/eu/netfxcompact/thread/66f6386a-9963-4c2f-8d39-1c507a26a6c7

Categories

Resources