Results 1 to 4 of 4

Thread: [RESOLVED] using dataAdapter with dataTable.

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] using dataAdapter with dataTable.

    I have a disconnected datatable that contains a few records. I am using the following function to get the dataTable.
    c# Code:
    1. static System.Data.DataTable ReadSetUpTable(string queryStr,SqlConnection sc)
    2.         {
    3.             try
    4.             {
    5.                 var command = new SqlCommand()
    6.                                   {Connection = sc, CommandText = queryStr};
    7.                 var dataAdapter = new SqlDataAdapter() {SelectCommand = command};
    8.                 var dataTable = new System.Data.DataTable();
    9.                 dataAdapter.Fill(dataTable);
    10.                 return dataTable;
    11.             }
    12.             catch (Exception)
    13.             {
    14.                 throw;
    15.             }
    16.         }

    No issues so far. What I want to know is if there's an easy to populate this dataTable into another schema using a different connection string. For the sake of this post, assume that there is a table with two columns
    Create Table Student(StudentId int, StudentName varchar2(50));

    I wish to fill this table with the dataTable that I have in the above code.

    I could do it using a command Object and an insert statement. For example this code:
    c# Code:
    1. static int LoadDataTable(OracleConnection oc, System.Data.DataTable dataTable)
    2.         {
    3.             try
    4.             {
    5.                 var command = new OracleCommand
    6.                                   {
    7.                                       CommandText = "INSERT INTO STUDENT (STUDENTID, STUDENTNAME) VALUES(:studentid, :studentname)",
    8.                                       CommandType = CommandType.TableDirect,
    9.                                       Connection = oc
    10.                                   };
    11.           var op1 = new OracleParameter
    12.                               {
    13.                                   ParameterName = "StudentId",
    14.                                   Size = 6,
    15.                                   OracleDbType = OracleDbType.Int,
    16.                                   Direction = System.Data.ParameterDirection.Input
    17.                               };
    18.                 command.Parameters.Add(op1);
    19.  
    20.                 var op2 = new OracleParameter
    21.                               {
    22.                                   ParameterName = "studentName",
    23.                                   OracleDbType = OracleDbType.Varchar2,
    24.                                   Size = 50,
    25.                                   Direction = System.Data.ParameterDirection.Input
    26.                               };
    27.                 command.Parameters.Add(op2);                                   
    28.                
    29.                 foreach (var row in dataTable.Rows)
    30.                 {
    31.                     command.Parameters("studentId").Value = int.Parse(row[0].ToString());
    32.                     command.Parameters("studentName").Value = row[1].ToString();
    33.                     command.ExecuteNonQuery();
    34.                 }    
    35.             }
    36.             catch(Exception)
    37.             {
    38.                 throw;
    39.             }
    40.            
    41.         }


    Is there a quicker and easier way where I will have to loop through the records?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: using dataAdapter with dataTable.

    So, basically you're asking how to copy data from one DataTable to another, correct? If so, there are various ways. You can simply loop through the Rows collection and get the data you want, Adding rows to the other table as you go. You can use a DataTableReader to read the table. You can use a LINQ query to read the desired data and either loop through it and populate an existing table or have it generate a new one. You can also generate a new DataTable from a DataView based on the first table.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: using dataAdapter with dataTable.

    Quote Originally Posted by jmcilhinney View Post
    So, basically you're asking how to copy data from one DataTable to another, correct? If so, there are various ways. You can simply loop through the Rows collection and get the data you want, Adding rows to the other table as you go. You can use a DataTableReader to read the table. You can use a LINQ query to read the desired data and either loop through it and populate an existing table or have it generate a new one. You can also generate a new DataTable from a DataView based on the first table.
    jmc,
    So far I have tried the following piece of code to avoid adding each row.
    C# Code:
    1. static int LoadDataTable(OracleConnection oc, System.Data.DataTable dataTable)
    2. {
    3.     try
    4.     {
    5.         var command =
    6.             new OracleCommand
    7.             {
    8.                 CommandText = "INSERT INTO STUDENT (STUDENTID, STUDENTNAME) VALUES(:studentid, :studentname)",
    9.                 CommandType = CommandType.TableDirect,
    10.                 Connection = oc
    11.             };
    12.         var op1 =
    13.             new OracleParameter
    14.             {
    15.                 ParameterName = "StudentId",
    16.                 Size = 6,
    17.                 OracleDbType = OracleDbType.Int32,
    18.             Direction = System.Data.ParameterDirection.Input
    19.             };
    20.         command.Parameters.Add(op1);
    21.         var op2 =
    22.         new OracleParameter
    23.             {
    24.                 ParameterName = "studentName",
    25.                 OracleDbType = OracleDbType.Varchar2,
    26.                 Size = 50,
    27.                 Direction = System.Data.ParameterDirection.Input
    28.             };
    29.         command.Parameters.Add(op2);                                  
    30.        /*
    31.         foreach (var row in dataTable.Rows)
    32.         {
    33.             op1.Value = int.Parse(row[0].ToString());
    34.             op2.Value = row[1].ToString();
    35.             command.ExecuteNonQuery();
    36.         }*/
    37.             foreach (System.Data.DataRow row in dataTable.Rows)
    38.             {
    39.                 row.SetAdded();
    40.             }    
    41.  
    42.             var dataAdapter = new OracleDataAdapter() {InsertCommand = command};
    43.             dataAdapter.Update(dataTable); //This updates the table, but all column values are NULL.
    44.  
    45.     }
    46.     catch(Exception)
    47.     {
    48.         throw;
    49.     }
    50. }

    Something's not all cop with this code though, as it creates rows but all the column values are Null.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: using dataAdapter with dataTable.

    I have fixed the above code by making a few changes. This is now resolved.
    c# Code:
    1. static int LoadDataTable(OracleConnection oc, System.Data.DataTable dataTable)
    2.     {
    3.         try
    4.         {
    5.             var command =
    6.                 new OracleCommand
    7.                 {
    8.                     CommandText = "INSERT INTO STUDENT (STUDENTID, STUDENTNAME) VALUES(:studentid, :studentname)",
    9.                     CommandType = CommandType.TableDirect,
    10.                     Connection = oc
    11.                 };
    12.             var op1 =
    13.                 new OracleParameter
    14.                 {
    15.                     ParameterName = "StudentId",
    16.                     Size = 6,
    17.                     OracleDbType = OracleDbType.Int32,
    18.                 Direction = System.Data.ParameterDirection.Input,
    19.                 SourceColumn="StudentId"
    20.                 };
    21.             command.Parameters.Add(op1);
    22.             var op2 =
    23.             new OracleParameter
    24.                 {
    25.                     ParameterName = "studentName",
    26.                     OracleDbType = OracleDbType.Varchar2,
    27.                     Size = 50,
    28.                     Direction = System.Data.ParameterDirection.Input,
    29.                     SourceColumn="StudentName"
    30.                 };
    31.             command.Parameters.Add(op2);                                  
    32.            /*
    33.             foreach (var row in dataTable.Rows)
    34.             {
    35.                 op1.Value = int.Parse(row[0].ToString());
    36.                 op2.Value = row[1].ToString();
    37.                 command.ExecuteNonQuery();
    38.             }*/
    39.                 foreach (System.Data.DataRow row in dataTable.Rows)
    40.                 {
    41.                     row.SetAdded();
    42.                 }  
    43.                 var dataAdapter = new OracleDataAdapter() {InsertCommand = command};
    44.                 dataAdapter.Update(dataTable); //This updates the table, but all column values are NULL.
    45.         }
    46.         catch(Exception)
    47.         {
    48.             throw;
    49.         }
    50.     }

    I had to add a source column property to the parameter.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width