C# .NET + PostgreSQL

后端 未结 9 1783
执念已碎
执念已碎 2021-01-30 05:17

I\'m looking at working on a project which uses C#.NET (sitting on a windows box) as the primary language and PostgreSQL as the backend database (backend is sitting on a linux b

9条回答
  •  温柔的废话
    2021-01-30 05:55

    Just Go to Tool-->NuGet Package Manager-->Manager Nuget Package Manager

    search for NpgSql and then select your project and click Install

    sample Code

    public void Demo()
            {
                NpgsqlConnection connection = new NpgsqlConnection();
                connection = d_connection; // your connection string
                connection.Open();              
                NpgsqlCommand cmd = new NpgsqlCommand();
                try
                {
                    cmd.Connection = connection;
                    cmd.CommandText = "select * from your table name";
                    cmd.CommandType = System.Data.CommandType.Text;
                    using (var dataReader = cmd.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
    
                         string answer= dataReader.IsDBNull(0) ? "" : dataReader.GetString(0);
    
                        }
                        dataReader.Dispose();
                    }
                }
                catch (Exception e)
                {
                }
                finally
                {
                    cmd.Dispose();
                    connection.Dispose();
                }            
            }
    

    Dont use upper case in postgreSql because its case sensitive.

提交回复
热议问题