none
DataReader vs. DataAdapter RRS feed

  • Question

  • hello,

    I have a problem to understand the DataReader. There is a temp table in a database, I search some values and I have to insert the values in a other (permanent) table. Now I read the values with the Datareader from the temp table like this


    sql_IT = "SELECT WeKdnr FROM dbo.tbl_temp_a WHERE Amount <=25 GROUP BY WeKdnr";

    cmd.CommandText = sql_IT;

    DbDataReader reader = cmd.ExecuteReader();

    while (reader.Read())

    {

    Console.WriteLine(reader["WeKdnr"].ToString());

    }

    reader.Close();

    sql_searchTester = "INSERT INTO dbo.tbl_wt VALUES ("+reader+")";

    cmd.CommandText = sql_searchTester;

    cmd.ExecuteNonQuery();


    How I get the values in a variable and insert it in the other table. thx a lot for your help

    Wednesday, April 26, 2006 7:43 AM

Answers

  • Hi,

    First you need to store your values somewhere - perhaps an ArrayList (.net 1.1) or List<> (.net 2).

    And then you will have to insert the values stored in the list one by one.

    Or, you might consider using a dataadapter/datatable approach. Combined with batching will yield better performance with less coding.

    Wednesday, April 26, 2006 8:05 AM

All replies

  • Hi,

    First you need to store your values somewhere - perhaps an ArrayList (.net 1.1) or List<> (.net 2).

    And then you will have to insert the values stored in the list one by one.

    Or, you might consider using a dataadapter/datatable approach. Combined with batching will yield better performance with less coding.

    Wednesday, April 26, 2006 8:05 AM
  • thx for form your fast answer, I will try it with a dataadapter.  I try this code


    SqlDataAdapter sql_select3 = new SqlDataAdapter("SELECT WeKdnr FROM dbo.tbl_temp_a WHERE Amount <=25 GROUP BY WeKdnr", conn);

    SqlCommand icmd = new SqlCommand("INSERT INTO dbo.tbl_wt (name) VALUES (?)", conn);

    icmd.Parameters.Add(new SqlParameter("WeKdnr", SqlDbType.Char,20));

    sql_select3.InsertCommand=icmd;


    But i did't work

    thx for every help ;o)

    Wednesday, April 26, 2006 8:17 AM
  • Now i use an other way. For fill data from one table into a other I just use sql
    sql_InsertTester = "INSERT INTO dbo.tbl_wt "

    + "SELECT wekdnr "

    + "FROM dbo.tbl_a "

    + "WHERE amount<=25 AND "

    + "wekdnr Not IN (SELECT name FROM dbo.tbl_wt "

    + "GROUP BY name) "

    + "GROUP BY wekdnr";


    and this work realy good. thx for help

    dabro

    Thursday, April 27, 2006 12:02 PM