如何使用 c# 连接 postgres 数据库 visual studio
How to connect postgres database with c# visual studio
所以,大学讲师给了我们一个任务,用 c# 程序连接 postgresSql 数据库。到目前为止,他们看起来像这样。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Common;
using Npgsql;
using NpgsqlTypes;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string postgresConStr = "Server=localhost;Port=5432;UserId=ricards;Password=muzika23;Database=prog4;";
NpgsqlConnection ncon = new NpgsqlConnection(postgresConStr);
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM persona", ncon);
ncon.Open();
NpgsqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("vards", typeof(string)));
dt.Columns.Add(new DataColumn("uzvards", typeof(string)));
dt.Columns.Add(new DataColumn("vecums", typeof(int)));
while (dr.Read())
{
DataRow row = dt.NewRow();
row["ID"] = dr["ID"];
row["vards"] = dr["vards"];
row["uzvards"] = dr["uzvards"];
row["vecums"] = dr["vecums"];
dt.Rows.Add(row);
}
ncon.Close();
dt.AcceptChanges();
int izvele;
do
{
Console.WriteLine("0 - Beigt darbu");
Console.WriteLine("1 - Apskatīt tabulas datus");
Console.WriteLine("2 - Pievienot tabulai datus");
Console.WriteLine("3 - Labot tabulas datus");
Console.WriteLine("4 - Dzēst tabuas datus");
Console.Write("Jūsu izvēle: ");
izvele = int.Parse(Console.ReadLine());
switch (izvele)
{
case 1:
PrintTable(dt);
break;
case 2:
DataRow drw = dt.NewRow();
Console.Write("Ievadiet studenta vārdu: ");
string vards = Console.ReadLine();
drw["vards"] = vards;
Console.Write("Ievadiet uzvārdu: ");
string uzvards = Console.ReadLine();
drw["uzvards"] = uzvards;
Console.Write("Ievadiet vecumu: ");
int vecums = int.Parse(Console.ReadLine());
drw["vecums"] = vecums;
dt.Rows.Add(drw);
ApplyToDb(dt, ncon);
break;
default:
break;
}
} while (izvele !=0);
}
static void PrintTable(DataTable dt)
{
foreach (DataColumn dc in dt.Columns)
{
Console.Write(dc.ColumnName + " ");
}
Console.WriteLine();
Console.WriteLine();
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dr.ItemArray.Length; i++)
{
Console.Write(dr[i] + "\t");
}
Console.WriteLine();
Console.WriteLine();
}
}
static void ApplyToDb(DataTable dt, NpgsqlConnection ncon)
{
NpgsqlCommand update = new NpgsqlCommand("UPDATE persona SET vards=?,uzvards=?, vecums=? WHERE ID =? ", ncon);
update.Parameters.Add(new NpgsqlParameter("@vardss", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@uzvards", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@vecums", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@ID", OleDbType.Integer));
NpgsqlCommand add = new NpgsqlCommand("INSERT INTO persona (vards, uzvards, vecums) VALUES(@vards, @uzvards, @vecums)", ncon);
add.Parameters.Add(new NpgsqlParameter("@vards", OleDbType.VarChar));
add.Parameters.Add(new NpgsqlParameter("@uzvards", OleDbType.VarChar));
add.Parameters.Add(new NpgsqlParameter("@vecums", OleDbType.VarChar));
foreach (DataRow dro in dt.GetChanges().Rows)
{
if (dro.RowState == DataRowState.Added)
{
add.Parameters[0].Value = dro[1];
add.Parameters[1].Value = dro[2];
add.Parameters[2].Value = dro[3];
ncon.Open();
add.ExecuteNonQuery();
ncon.Close();
}
if (dro.RowState == DataRowState.Modified)
{
update.Parameters[0].Value = dro[1];
update.Parameters[1].Value = dro[2];
update.Parameters[2].Value = dro[3];
update.Parameters[3].Value = dro[0];
ncon.Open();
update.ExecuteNonQuery();
ncon.Close();
}
}
}
}
我确定代码不是问题,我检查了 connectionStrings 路径,它们都与 postgres(主机、数据库、用户等)匹配。
我想知道的是,我是否必须将该 postgres 数据库放在 c# 程序调试文件夹中或什么?教授没提过吗?而且,我什至如何找到该数据库,我搜索了整个 Postgres 文件夹,但似乎无法找到隐藏数据库的位置。
请帮帮我。
谢谢
编辑
update.Parameters.Add(new NpgsqlParameter("@vardss", OleDbType.VarChar));
我需要将 OleDbType 更改为 postgres 类型吗?怎么做? (这部分 - OleDbType.VarChar)
没有postgres精简版。
你需要一个postgres服务器,你可以从postgres site, or if you have some hyper-v, vmware, virtualbox app, you can download one virtual appliance from turnkey下载。
所以,大学讲师给了我们一个任务,用 c# 程序连接 postgresSql 数据库。到目前为止,他们看起来像这样。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Common;
using Npgsql;
using NpgsqlTypes;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string postgresConStr = "Server=localhost;Port=5432;UserId=ricards;Password=muzika23;Database=prog4;";
NpgsqlConnection ncon = new NpgsqlConnection(postgresConStr);
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM persona", ncon);
ncon.Open();
NpgsqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("vards", typeof(string)));
dt.Columns.Add(new DataColumn("uzvards", typeof(string)));
dt.Columns.Add(new DataColumn("vecums", typeof(int)));
while (dr.Read())
{
DataRow row = dt.NewRow();
row["ID"] = dr["ID"];
row["vards"] = dr["vards"];
row["uzvards"] = dr["uzvards"];
row["vecums"] = dr["vecums"];
dt.Rows.Add(row);
}
ncon.Close();
dt.AcceptChanges();
int izvele;
do
{
Console.WriteLine("0 - Beigt darbu");
Console.WriteLine("1 - Apskatīt tabulas datus");
Console.WriteLine("2 - Pievienot tabulai datus");
Console.WriteLine("3 - Labot tabulas datus");
Console.WriteLine("4 - Dzēst tabuas datus");
Console.Write("Jūsu izvēle: ");
izvele = int.Parse(Console.ReadLine());
switch (izvele)
{
case 1:
PrintTable(dt);
break;
case 2:
DataRow drw = dt.NewRow();
Console.Write("Ievadiet studenta vārdu: ");
string vards = Console.ReadLine();
drw["vards"] = vards;
Console.Write("Ievadiet uzvārdu: ");
string uzvards = Console.ReadLine();
drw["uzvards"] = uzvards;
Console.Write("Ievadiet vecumu: ");
int vecums = int.Parse(Console.ReadLine());
drw["vecums"] = vecums;
dt.Rows.Add(drw);
ApplyToDb(dt, ncon);
break;
default:
break;
}
} while (izvele !=0);
}
static void PrintTable(DataTable dt)
{
foreach (DataColumn dc in dt.Columns)
{
Console.Write(dc.ColumnName + " ");
}
Console.WriteLine();
Console.WriteLine();
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dr.ItemArray.Length; i++)
{
Console.Write(dr[i] + "\t");
}
Console.WriteLine();
Console.WriteLine();
}
}
static void ApplyToDb(DataTable dt, NpgsqlConnection ncon)
{
NpgsqlCommand update = new NpgsqlCommand("UPDATE persona SET vards=?,uzvards=?, vecums=? WHERE ID =? ", ncon);
update.Parameters.Add(new NpgsqlParameter("@vardss", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@uzvards", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@vecums", OleDbType.VarChar));
update.Parameters.Add(new NpgsqlParameter("@ID", OleDbType.Integer));
NpgsqlCommand add = new NpgsqlCommand("INSERT INTO persona (vards, uzvards, vecums) VALUES(@vards, @uzvards, @vecums)", ncon);
add.Parameters.Add(new NpgsqlParameter("@vards", OleDbType.VarChar));
add.Parameters.Add(new NpgsqlParameter("@uzvards", OleDbType.VarChar));
add.Parameters.Add(new NpgsqlParameter("@vecums", OleDbType.VarChar));
foreach (DataRow dro in dt.GetChanges().Rows)
{
if (dro.RowState == DataRowState.Added)
{
add.Parameters[0].Value = dro[1];
add.Parameters[1].Value = dro[2];
add.Parameters[2].Value = dro[3];
ncon.Open();
add.ExecuteNonQuery();
ncon.Close();
}
if (dro.RowState == DataRowState.Modified)
{
update.Parameters[0].Value = dro[1];
update.Parameters[1].Value = dro[2];
update.Parameters[2].Value = dro[3];
update.Parameters[3].Value = dro[0];
ncon.Open();
update.ExecuteNonQuery();
ncon.Close();
}
}
}
}
我确定代码不是问题,我检查了 connectionStrings 路径,它们都与 postgres(主机、数据库、用户等)匹配。 我想知道的是,我是否必须将该 postgres 数据库放在 c# 程序调试文件夹中或什么?教授没提过吗?而且,我什至如何找到该数据库,我搜索了整个 Postgres 文件夹,但似乎无法找到隐藏数据库的位置。
请帮帮我。
谢谢
编辑
update.Parameters.Add(new NpgsqlParameter("@vardss", OleDbType.VarChar));
我需要将 OleDbType 更改为 postgres 类型吗?怎么做? (这部分 - OleDbType.VarChar)
没有postgres精简版。
你需要一个postgres服务器,你可以从postgres site, or if you have some hyper-v, vmware, virtualbox app, you can download one virtual appliance from turnkey下载。