当我尝试连接到DataBase时,出现错误:关键字不支持:主机。使用带有dotConnect的Visual Studio无法连接到postrgres
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
你为什么不使用'NpgsqlConnection'(或者' OdbcConnection'?)而不是'SqlConnection'? SQL Server提供程序具有“数据源”,而不是“主机”。还可以使用'using'语句来处理连接和命令automaticalliy,而不是手动调用'Close'方法。 –
为什么不使用[Npgsql](http://www.npgsql.org/doc/)?,只要看看这个[教程](http://www.codeproject.com/Articles/30989/Using- PostgreSQL-in-your-C-NET-application-An-intr) –
@wingedpanther谢谢你,连接工作,但我得到了这个结果“输出参数'@k'的值在命令执行结果中不存在。参数的名称:@k –