1

I have a problem I am an average developer of .net applications as I was working in PHP earlier.

I want to insert a populated DataTable directly to Database through ODBC in .net language is C#

Can anyone please help me doing this .

Thank you so much

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

//code for 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();



    }

I am stuck here a little bit what to do .. Please guide

1 Answer 1

1

Have a look at .Net Data Provider for Postgresql

And more specifically at the function

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

The application which I am working was written in vb6 the purpose of that application was to synchronize electronic cards generated on different gates to the central database . I have rewritten the code in C# using .net 4.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.