none
Problem with DataTable.Tablename RRS feed

  • Question

  • Hi all

    Can anyone tell me, why this code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;
    
    namespace MsAccess {
        class Program {
            static void Main(string[] args) {
                string c = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PdfDataBase.accdb";
                OleDbConnection connection = new OleDbConnection(c);
    
                OleDbCommand selectCmd = new OleDbCommand("SELECT Pdf_ID, Filepath, Filename FROM PdfFiles ORDER BY Pdf_ID", connection);
    
                OleDbDataAdapter adapter = new OleDbDataAdapter(selectCmd);
                DataSet dataset = new DataSet();
                OleDbCommandBuilder b = new OleDbCommandBuilder(adapter);
                adapter.DeleteCommand = b.GetDeleteCommand(true);
                adapter.InsertCommand = b.GetInsertCommand(true);
                adapter.UpdateCommand = b.GetUpdateCommand(true);
    
                adapter.FillSchema(dataset, SchemaType.Source);
                Console.WriteLine(adapter.Fill(dataset));
                dataset.Tables[0].Columns[0].AutoIncrement = true;
    
                DataRow row = dataset.Tables[0].NewRow();
                row[1] = "Test";
                dataset.Tables[0].Rows.Add(row);
    
                Console.WriteLine(adapter.Update(dataset));
                Console.ReadLine();
            }
        }
    }
    


    is running correctly and this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;
    
    namespace MsAccess {
        class Program {
            static void Main(string[] args) {
                string c = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PdfDataBase.accdb";
                OleDbConnection connection = new OleDbConnection(c);
    
                OleDbCommand selectCmd = new OleDbCommand("SELECT Pdf_ID, Filepath, Filename FROM PdfFiles ORDER BY Pdf_ID", connection);
    
                OleDbDataAdapter adapter = new OleDbDataAdapter(selectCmd);
                DataSet dataset = new DataSet();
                OleDbCommandBuilder b = new OleDbCommandBuilder(adapter);
                adapter.DeleteCommand = b.GetDeleteCommand(true);
                adapter.InsertCommand = b.GetInsertCommand(true);
                adapter.UpdateCommand = b.GetUpdateCommand(true);
    
                adapter.FillSchema(dataset, SchemaType.Source, "PdfFiles");
                adapter.TableMappings.Add("PdfFiles", "PdfFiles");
                Console.WriteLine(adapter.Fill(dataset));
                dataset.Tables[0].Columns[0].AutoIncrement = true;
    
                DataRow row = dataset.Tables["PdfFiles"].NewRow();
                row[1] = "Test";
                dataset.Tables["PdfFiles"].Rows.Add(row);
    
                Console.WriteLine(adapter.Update(dataset));
                Console.ReadLine();
            }
        }
    }
    

    not!!

    The only difference that I can see, is that in the second code my DataTable.TableName is different from 'Table' (Default).

    Please help me.

    Thanks

    Saturday, January 12, 2013 1:17 AM

Answers

  • I've found the mistake by myself.

    Here's to correct code:

                string c = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PdfDataBase.accdb";
                OleDbConnection connection = new OleDbConnection(c);
    
                OleDbCommand selectCmd = new OleDbCommand("SELECT Pdf_ID, Filepath, Filename FROM PdfFiles ORDER BY Pdf_ID", connection);
    
                OleDbDataAdapter adapter = new OleDbDataAdapter(selectCmd);
                DataSet dataset = new DataSet();
    
                adapter.FillSchema(dataset, SchemaType.Source, "PdfFiles");
                adapter.TableMappings.Add("PdfFiles", "PdfFiles");
                Console.WriteLine("Data loaded: {0}", adapter.Fill(dataset.Tables["PdfFiles"]));
                OleDbCommandBuilder b = new OleDbCommandBuilder(adapter);
                adapter.DeleteCommand = b.GetDeleteCommand(true);
                adapter.InsertCommand = b.GetInsertCommand(true);
                adapter.UpdateCommand = b.GetUpdateCommand(true);
                dataset.Tables[0].Columns[0].AutoIncrement = true;
    
                DataRow row = dataset.Tables["PdfFiles"].NewRow();
                row[1] = "Test";
                dataset.Tables["PdfFiles"].Rows.Add(row);
    
                Console.WriteLine("Data saved: {0}", adapter.Update(dataset.Tables["PdfFiles"]));

    When I want to use DataTable with TableName's, I have use this overload of the methods:
    adapter.Fill(dataset.Tables["PdfFiles"])

    adapter.Update(dataset.Tables["PdfFiles"])


    • Marked as answer by Koller91 Saturday, January 12, 2013 11:30 AM
    Saturday, January 12, 2013 9:10 AM

All replies

  • I've found the mistake by myself.

    Here's to correct code:

                string c = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PdfDataBase.accdb";
                OleDbConnection connection = new OleDbConnection(c);
    
                OleDbCommand selectCmd = new OleDbCommand("SELECT Pdf_ID, Filepath, Filename FROM PdfFiles ORDER BY Pdf_ID", connection);
    
                OleDbDataAdapter adapter = new OleDbDataAdapter(selectCmd);
                DataSet dataset = new DataSet();
    
                adapter.FillSchema(dataset, SchemaType.Source, "PdfFiles");
                adapter.TableMappings.Add("PdfFiles", "PdfFiles");
                Console.WriteLine("Data loaded: {0}", adapter.Fill(dataset.Tables["PdfFiles"]));
                OleDbCommandBuilder b = new OleDbCommandBuilder(adapter);
                adapter.DeleteCommand = b.GetDeleteCommand(true);
                adapter.InsertCommand = b.GetInsertCommand(true);
                adapter.UpdateCommand = b.GetUpdateCommand(true);
                dataset.Tables[0].Columns[0].AutoIncrement = true;
    
                DataRow row = dataset.Tables["PdfFiles"].NewRow();
                row[1] = "Test";
                dataset.Tables["PdfFiles"].Rows.Add(row);
    
                Console.WriteLine("Data saved: {0}", adapter.Update(dataset.Tables["PdfFiles"]));

    When I want to use DataTable with TableName's, I have use this overload of the methods:
    adapter.Fill(dataset.Tables["PdfFiles"])

    adapter.Update(dataset.Tables["PdfFiles"])


    • Marked as answer by Koller91 Saturday, January 12, 2013 11:30 AM
    Saturday, January 12, 2013 9:10 AM
  • Hi Koller,

    Glad to know your problem is solved. Thank you for sharing.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 14, 2013 1:33 AM