首先新建项目后引用Npgsql,定义连接参数
string
strConn
=
@"
SERVER=127.0.0.1;DATABASE=template_postgis;USERID=postgres;PASSWORD=111
"
;
private void btnConnectPostsql_Click( object sender, EventArgs e)
{
string sql = " INSERT INTO table_name (the_geom) VALUES (ST_GeomFromText('MULTILINESTRING((1 1,2 3,3 4,3 2),(3 2,3 1))',-1)); " ;
ExecuteScalar(sql)
}
public object ExecuteScalar( string sql)
{
IDbConnection dbcon;
dbcon = new NpgsqlConnection (strConn);
using (dbcon)
{
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sql;
object ob = dbcmd.ExecuteScalar();
dbcon.Close();
return ob;
}
}
private void btnConnectPostsql_Click( object sender, EventArgs e)
{
string sql = " INSERT INTO table_name (the_geom) VALUES (ST_GeomFromText('MULTILINESTRING((1 1,2 3,3 4,3 2),(3 2,3 1))',-1)); " ;
ExecuteScalar(sql)
}
public object ExecuteScalar( string sql)
{
IDbConnection dbcon;
dbcon = new NpgsqlConnection (strConn);
using (dbcon)
{
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = sql;
object ob = dbcmd.ExecuteScalar();
dbcon.Close();
return ob;
}
}