当我试图连接到DataBase时,会出现错误:关键字不支持:主机。
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);
}连接字符串:
User Id=postgres;Password=8loz9fnl;Host=localhost;Database=postgres;Persist Security Info=True

发布于 2015-12-21 10:32:53
我猜您正在使用devart的dotconnect来连接ado.net,所以您应该导入Devart.Data.PostgreSql,使用PgSqlConnection而不是sqlconnection和pgsqlcommand来代替sqlcommand
发布于 2015-12-21 10:12:22
正如其他人已经提到的,您需要使用Npgsql。还可以使用导入来简化代码。示例连接代码:导入程序集using Npgsql;,然后获取连接并打开它
using (var conn = new NpgsqlConnection(Properties.Settings.Default.postgresConnectionString))
{
// you other code here
}通过这种方式,您的资源将被自动管理,您不需要调用close方法,您还需要更改命令的类型,就像@wingedpanther提到的那样。读一本好的教程或上面提到的文档会有很大帮助。此外,您还需要将连接字符串修复为:
Server=localhost;User Id=postgres;Password=8loz9fnl;Database=postgres;Persist Security Info=True并避免放置您的实际用户名和密码。
https://stackoverflow.com/questions/34392898
复制相似问题