1

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

我想通过 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();



    }

我被困在这里有点该怎么办..请指导

4

1 回答 1

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

}
于 2013-08-17T06:31:56.737 回答