通过ODBC直接将DataTable插入PostGres数据库

时间:2013-08-17 06:26:52

标签: c# .net database postgresql

我有一个问题我是.net应用程序的普通开发人员,因为我之前在PHP工作。

我想在.net语言中通过ODBC将填充的DataTable直接插入数据库是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);

// TransactionInsertion的代码

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();



    }

我被困在这里一点点做什么..请指导

1 个答案:

答案 0 :(得分:1)

查看.Net Data Provider for Postgresql

更具体地说是功能

// 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";

}