Artem Gunkin
Artem Gunkin

Reputation: 61

Can't connect to postrgres using Visual Studio with dotConnect

When I try to connect to DataBase, I get error: Key word doesn't support: Host.

        int x = Int32.Parse(textBox1.Text);
        try
        {

            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString =
           Properties.Settings.Default.postgresConnectionString;
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "getCount";
            System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter("@k",
           SqlDbType.Int);
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(param);
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Tovar", x));
            con.Open();
            cmd.ExecuteNonQuery();
            string kolvo = cmd.Parameters["@k"].Value.ToString();
            con.Close();
            label1.Text = kolvo + " израсходован в количестве ";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Connection String:

User Id=postgres;Password=8loz9fnl;Host=localhost;Database=postgres;Persist Security Info=True

enter image description here

Upvotes: 0

Views: 534

Answers (2)

Vivek S.
Vivek S.

Reputation: 21915

I guess that you're using devart's dotconnect for ado.net connectivity so you should import Devart.Data.PostgreSql and use PgSqlConnection instead of sqlconnection and pgsqlcommand instead of sqlcommand

Upvotes: 2

zulqarnain
zulqarnain

Reputation: 1735

As others have mentioned already, you need to use Npgsql. Also use imports to simplify your code. Example connection code: import the assembly using Npgsql; then to get a connection and open it

using (var  conn = new NpgsqlConnection(Properties.Settings.Default.postgresConnectionString)) 
{ 
    // you other code here
}

This way your resources are managed automatically and you don't need to call the close method, also you need to change the type of command as @wingedpanther mentions. Reading a good tutorial or the docs mentioned will help a lot. Also you need to fix your connection string as:

Server=localhost;User Id=postgres;Password=8loz9fnl;Database=postgres;Persist Security Info=True

And avoid putting your actual username and password.

Upvotes: 0

Related Questions