數(shù)據(jù)庫 主要是PostgreSQL15
開發(fā)軟件 Visual Studio 2012
C#用到DLL是 Npgsql.dll
二.連接數(shù)據(jù)庫連接串
Server=數(shù)據(jù)庫IP;Port=5432;User Id=用戶名;Password=密碼;Database=數(shù)據(jù)庫;
Server是數(shù)據(jù)庫IP
Port端口5432(默認(rèn))
User Id用戶名
Password密碼
Database數(shù)據(jù)庫名
三.執(zhí)行查詢語句,返回DataSet
第一步創(chuàng)建數(shù)據(jù)庫連接
NpgsqlConnection connection = new NpgsqlConnection(PGConnection)
PGConnection 為連接數(shù)據(jù)庫連接串
第二步打開數(shù)據(jù)庫連接
connection.Open();
第三步執(zhí)行 使用NpgsqlCommand構(gòu)造SQL語句
NpgsqlCommand command = new NpgsqlCommand(SQLString, connection);
第四步執(zhí)行 查詢語句 返回 NpgsqlDataAdapter類
NpgsqlDataAdapter npgdata = new NpgsqlDataAdapter(command);
第五步 NpgsqlDataAdapter填充DataSet
npgdata.Fill(ds);
第六步關(guān)閉數(shù)據(jù)庫連接
connection.Close();
四.執(zhí)行修改 刪除 創(chuàng)建語句,返回是否成功
第一步創(chuàng)建數(shù)據(jù)庫連接
NpgsqlConnection connection = new NpgsqlConnection(PGConnection)
PGConnection 為連接數(shù)據(jù)庫連接串
第二步打開數(shù)據(jù)庫連接
connection.Open();
第三步執(zhí)行 使用NpgsqlCommand類 執(zhí)行數(shù)據(jù)庫執(zhí)行的 SQL 語句
NpgsqlCommand command = new NpgsqlCommand(SQLString, connection);
第四步 獲取受影響的行數(shù)
int recordCount = command.ExecuteNonQuery();
第五步關(guān)閉數(shù)據(jù)庫連接
connection.Close();
閱讀原文:原文鏈接
該文章在 2025/7/7 11:55:17 編輯過