通过ODBC将DataTable直接插入PostGres数据库
本文关键字:PostGres 数据库 直接插入 DataTable ODBC 通过 | 更新日期: 2023-09-27 18:27:57
我有一个问题,我是.net应用程序的普通开发人员,因为我以前使用PHP。
我想通过ODBC将填充的DataTable直接插入数据库。net语言是C#
有人能帮我做这个吗。
非常感谢
if (_DataTable1.Rows.Count > 0)
{
int j=0;
for(int i =0 ; i < _DataTable1.Rows.Count ; i ++){
query = "Select * from Cards where ecode='" + _DataTable1.Rows[i]["ecode"] + "'";
_DataTable2 = CentralConn.Select(query);
if (_DataTable2.Rows.Count > 0)
{
_DataTableUpdate.Rows.Add(DUpdateRow);
foreach (string name in Enum.GetNames(typeof(LocalToCentralFields)))
{
_DataTableUpdate.Rows[j][name] = _DataTable1.Rows[i][name];
}
j++;
}
else {
_DataTableInsert.Rows.Add(DInsertRow);
foreach (string name in Enum.GetNames(typeof(LocalToCentralFields)))
{
_DataTableInsert.Rows[j][name] = _DataTable1.Rows[i][name];
}
j++;
}
}
CentralConn.TransactionInsertion(_DataTableInsert);
//事务插入代码
public void TransactionInsertion(DataTable DT,string TableName)
{
OdbcCommand Command = connection.CreateCommand();
Command.CommandText = "Insert";
Transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
OdbcDataAdapter Adapter = new OdbcDataAdapter();
Adapter.InsertCommand = new OdbcCommand();
}
我有点不知所措该怎么办。。请引导
看看Postgresql的.Net数据提供程序
更具体地说,在功能
// Setups the insert command.
public void insertCmd()
{
string insertQuery = "INSERT INTO npdata VALUES (@key, @ndata)";
NpAdapter.InsertCommand = new NpgsqlCommand(insertQuery, conn);
NpParam = NpAdapter.InsertCommand.Parameters.Add("@key", NpgsqlTypes.NpgsqlDbType.Text);
NpParam.SourceColumn = "key";
NpParam.SourceVersion = DataRowVersion.Current;
NpParam = NpAdapter.InsertCommand.Parameters.Add("@ndata", NpgsqlTypes.NpgsqlDbType.Bigint);
NpParam.SourceVersion = DataRowVersion.Current;
NpParam.SourceColumn = "ndata";
}