建立C#和PostgreSQL之间的连接

时间:2018-01-02 06:10:04

标签: c# postgresql

我正在尝试从C#窗口添加长度,宽度,区域等参数值 应用于PostgreSQL中的表。我是初学者。 我收到以下错误, 后端发送了无法识别的响应类型:e

private void button1_Click(object sender, EventArgs e)
        {
            int length, breadth;float area;
            length=Convert.ToInt32(Length.Text);
            breadth = Convert.ToInt32(Breadth.Text);
            area = length * breadth;
            Area.Text = area.ToString();
            string connectionString = "Server=localhost;port=5432;Database=DatabaseName;User Id=postgres;Password=pswd";

        NpgsqlConnection dbcon = new NpgsqlConnection(connectionString);
        dbcon.Open();

        NpgsqlCommand dbcmd = dbcon.CreateCommand();

        try
        {
            dbcmd.Parameters.AddWithValue("@len", length);
            dbcmd.Parameters.AddWithValue("@bth", breadth);
            dbcmd.Parameters.AddWithValue("@area", area);
            dbcmd.CommandText = "INSERT INTO Area(length,breadth,area) VALUES (@len,@bth,@area)";

            //dbcmd.CommandText = sql1;
            //dbcmd.Parameters.Add("@textBox3.Text", textBox3.Text.Trim());
            dbcmd.ExecuteNonQuery();
            dbcmd.Dispose();
            dbcon.Close();
        }
        catch (NpgsqlException ex)
        {
            MessageBox.Show("there is a error");
            if (ex.Data == null)
            {
                throw;
            }
            else
            {

            }
        }
    }

0 个答案:

没有答案