Another try - inserting datset into sql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a

    Another try - inserting datset into sql

    NEW Post

    Here's my best guess at how to insert this dataset....
    the code runs, but no new records are added to the sql table.

    I've read and split a delimited text file into a dataset.
    It looks fine in a datagrid (5 columns and 5,000 rows),
    but I've been trying, without success, to then insert
    the resulting dataset called "result" into a single sql
    table that has an auto-increment and PK column called ID,
    as well as the 5 columns from the dataset.

    Any suggestions on a way to perform the insert of the
    "result" dataset into the sql table?

    Thanks,

    Paul

    =============== =============== =============== =============== =============== =============== ========


    StreamReader sr = new StreamReader("C :\\test.txt"); //Read From A
    File instead of a webrequest

    DataSet result = new DataSet(); //The DataSet to Return
    result.Tables.A dd("MyNewTable" ); //Add DataTable to hold the DataSet

    result.Tables["MyNewTable "].Columns.Add("C ompanyName"); //Add a single
    column to the DataTable
    result.Tables["MyNewTable "].Columns.Add("F ormType"); //Add a single
    column
    result.Tables["MyNewTable "].Columns.Add("C IK"); //Add a single
    column
    result.Tables["MyNewTable "].Columns.Add("D ateFiled"); //Add a single
    column
    result.Tables["MyNewTable "].Columns.Add("S ECWebAddress"); //Add a single
    column

    string AllData1 = sr.ReadToEnd(); //Read the rest of the
    data in the file.
    string[] rows = AllData1.Split( "\n".ToCharArra y()); //Split off each
    row at the Line Feed

    foreach(string r in rows) //Now add each row to the
    DataSet
    {
    string delimStr1 = "\t";
    string[] items = r.Split(delimSt r1.ToCharArray( )); //Split the row at the
    delimiter
    result.Tables["MyNewTable "].Rows.Add(items ); //Add the item
    }

    for (int i = 1; i <= 11; i++) //Remove first 8
    rows from the DataTable/DataSet
    {
    result.Tables["MyNewTable "].Rows.RemoveAt( 0);
    }

    dataGrid1.SetDa taBinding(resul t, "MyNewTable "); //Binds DataGrid to
    DataSet,display ing datatable

    SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter( ); //Configure a
    data adapter

    //Configure the SQL connection
    SqlConnection sqlConnection1 = new SqlConnection(" workstation id=AMD;packet
    size=4096;" +
    "integrated security=SSPI;d ata source=AMD;pers ist security
    info=False;init ial catalog=SEC_XBR L_10");

    //Generate the 'INSERT' command
    SqlCommand sqlInsertComman d1 = new SqlCommand("INS ERT INTO
    SEC_Index_01(Co mpanyName, FormType, CIK, " +
    "DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
    @DateFiled, @FileName); SELECT ID, " +
    "CompanyNam e, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
    WHERE (ID = @@IDENTITY)", sqlConnection1) ;

    //Add the parameters
    sqlInsertComman d1.Parameters.A dd("@CompanyNam e",
    System.Data.Sql DbType.VarChar, 8000, "CompanyNam e");
    sqlInsertComman d1.Parameters.A dd("@FormType" , System.Data.Sql DbType.VarChar,
    8000, "FormType") ;
    sqlInsertComman d1.Parameters.A dd("@CIK", System.Data.Sql DbType.VarChar,
    8000, "CIK");
    sqlInsertComman d1.Parameters.A dd("@DateFiled" ,
    System.Data.Sql DbType.VarChar, 8000, "DateFiled" );
    sqlInsertComman d1.Parameters.A dd("@FileName" , System.Data.Sql DbType.VarChar,
    8000, "SECWebAddress" );


    sqlDataAdapter1 .InsertCommand = sqlInsertComman d1; //Set the insert
    command

    sqlDataAdapter1 .Update(result, "SEC_Index_01") ; //Perform the
    update


  • W.G. Ryan eMVP

    #2
    Re: Another try - inserting datset into sql

    First off, check the Rowstate of the rows in question or test the dataset
    with Debug.Assert(my DataSet.HasChan ges());

    If this is false or the rowstate of everything is unchanged, then no matter
    how many times you call update, nothing will happen. A typical mistake taht
    causes this is calling AcceptChanges on the rows or the dataset sometime
    before calling Update.

    Next, you'll need to check your update command and make sure that it's valid
    and that the column mappings are correct and the parameters all match. For
    instance, if you hard coded an insert statement without parameters as your
    adapter's insert command, it will fire each time a Row with a rowstate of
    inserted is found - but since you hard coded the values, it won't
    dynamically change the values its inserting

    If you could post the code it would probably help to narrow things down.

    --
    W.G. Ryan, MVP

    www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
    "a" <a@discussions. microsoft.com> wrote in message
    news:A7C30267-7B5A-47D2-B278-7DE81032A468@mi crosoft.com...[color=blue]
    > NEW Post
    >
    > Here's my best guess at how to insert this dataset....
    > the code runs, but no new records are added to the sql table.
    >
    > I've read and split a delimited text file into a dataset.
    > It looks fine in a datagrid (5 columns and 5,000 rows),
    > but I've been trying, without success, to then insert
    > the resulting dataset called "result" into a single sql
    > table that has an auto-increment and PK column called ID,
    > as well as the 5 columns from the dataset.
    >
    > Any suggestions on a way to perform the insert of the
    > "result" dataset into the sql table?
    >
    > Thanks,
    >
    > Paul
    >
    >[/color]
    =============== =============== =============== =============== =============== =
    =============== =======[color=blue]
    >
    >
    > StreamReader sr = new StreamReader("C :\\test.txt"); //Read From A
    > File instead of a webrequest
    >
    > DataSet result = new DataSet(); //The DataSet to Return
    > result.Tables.A dd("MyNewTable" ); //Add DataTable to hold the DataSet
    >
    > result.Tables["MyNewTable "].Columns.Add("C ompanyName"); //Add a single
    > column to the DataTable
    > result.Tables["MyNewTable "].Columns.Add("F ormType"); //Add a single
    > column
    > result.Tables["MyNewTable "].Columns.Add("C IK"); //Add a single
    > column
    > result.Tables["MyNewTable "].Columns.Add("D ateFiled"); //Add a single
    > column
    > result.Tables["MyNewTable "].Columns.Add("S ECWebAddress"); //Add a single
    > column
    >
    > string AllData1 = sr.ReadToEnd(); //Read the rest of the
    > data in the file.
    > string[] rows = AllData1.Split( "\n".ToCharArra y()); //Split off[/color]
    each[color=blue]
    > row at the Line Feed
    >
    > foreach(string r in rows) //Now add each row to the
    > DataSet
    > {
    > string delimStr1 = "\t";
    > string[] items = r.Split(delimSt r1.ToCharArray( )); //Split the row at the
    > delimiter
    > result.Tables["MyNewTable "].Rows.Add(items ); //Add the item
    > }
    >
    > for (int i = 1; i <= 11; i++) //Remove first[/color]
    8[color=blue]
    > rows from the DataTable/DataSet
    > {
    > result.Tables["MyNewTable "].Rows.RemoveAt( 0);
    > }
    >
    > dataGrid1.SetDa taBinding(resul t, "MyNewTable "); //Binds DataGrid to
    > DataSet,display ing datatable
    >
    > SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter( ); //Configure a
    > data adapter
    >
    > //Configure the SQL connection
    > SqlConnection sqlConnection1 = new SqlConnection(" workstation[/color]
    id=AMD;packet[color=blue]
    > size=4096;" +
    > "integrated security=SSPI;d ata source=AMD;pers ist security
    > info=False;init ial catalog=SEC_XBR L_10");
    >
    > //Generate the 'INSERT' command
    > SqlCommand sqlInsertComman d1 = new SqlCommand("INS ERT INTO
    > SEC_Index_01(Co mpanyName, FormType, CIK, " +
    > "DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
    > @DateFiled, @FileName); SELECT ID, " +
    > "CompanyNam e, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
    > WHERE (ID = @@IDENTITY)", sqlConnection1) ;
    >
    > //Add the parameters
    > sqlInsertComman d1.Parameters.A dd("@CompanyNam e",
    > System.Data.Sql DbType.VarChar, 8000, "CompanyNam e");
    > sqlInsertComman d1.Parameters.A dd("@FormType" ,[/color]
    System.Data.Sql DbType.VarChar,[color=blue]
    > 8000, "FormType") ;
    > sqlInsertComman d1.Parameters.A dd("@CIK", System.Data.Sql DbType.VarChar,
    > 8000, "CIK");
    > sqlInsertComman d1.Parameters.A dd("@DateFiled" ,
    > System.Data.Sql DbType.VarChar, 8000, "DateFiled" );
    > sqlInsertComman d1.Parameters.A dd("@FileName" ,[/color]
    System.Data.Sql DbType.VarChar,[color=blue]
    > 8000, "SECWebAddress" );
    >
    >
    > sqlDataAdapter1 .InsertCommand = sqlInsertComman d1; //Set the[/color]
    insert[color=blue]
    > command
    >
    > sqlDataAdapter1 .Update(result, "SEC_Index_01") ; //Perform the
    > update
    >
    >[/color]


    Comment

    • a

      #3
      Re: Another try - inserting datset into sql

      That IS the code below the double dashed line...so what did i leave out?

      What's the simplest way to insert new rows from the dataset that I've
      created here into an existing sql table, with a dataadapter or can I just
      loop through the dataset somehow and append the rows to the sql table.

      I don't need anything fancy here, like validating or checking the existing
      rows
      against the new rows that I'm trying to insert

      Thanks,

      Paul
      ----------------------------------------------------------------

      "W.G. Ryan eMVP" wrote:
      [color=blue]
      > First off, check the Rowstate of the rows in question or test the dataset
      > with Debug.Assert(my DataSet.HasChan ges());
      >
      > If this is false or the rowstate of everything is unchanged, then no matter
      > how many times you call update, nothing will happen. A typical mistake taht
      > causes this is calling AcceptChanges on the rows or the dataset sometime
      > before calling Update.
      >
      > Next, you'll need to check your update command and make sure that it's valid
      > and that the column mappings are correct and the parameters all match. For
      > instance, if you hard coded an insert statement without parameters as your
      > adapter's insert command, it will fire each time a Row with a rowstate of
      > inserted is found - but since you hard coded the values, it won't
      > dynamically change the values its inserting
      >
      > If you could post the code it would probably help to narrow things down.
      >
      > --
      > W.G. Ryan, MVP
      >
      > www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
      > "a" <a@discussions. microsoft.com> wrote in message
      > news:A7C30267-7B5A-47D2-B278-7DE81032A468@mi crosoft.com...[color=green]
      > > NEW Post
      > >
      > > Here's my best guess at how to insert this dataset....
      > > the code runs, but no new records are added to the sql table.
      > >
      > > I've read and split a delimited text file into a dataset.
      > > It looks fine in a datagrid (5 columns and 5,000 rows),
      > > but I've been trying, without success, to then insert
      > > the resulting dataset called "result" into a single sql
      > > table that has an auto-increment and PK column called ID,
      > > as well as the 5 columns from the dataset.
      > >
      > > Any suggestions on a way to perform the insert of the
      > > "result" dataset into the sql table?
      > >
      > > Thanks,
      > >
      > > Paul
      > >
      > >[/color]
      > =============== =============== =============== =============== =============== =
      > =============== =======[color=green]
      > >
      > >
      > > StreamReader sr = new StreamReader("C :\\test.txt"); //Read From A
      > > File instead of a webrequest
      > >
      > > DataSet result = new DataSet(); //The DataSet to Return
      > > result.Tables.A dd("MyNewTable" ); //Add DataTable to hold the DataSet
      > >
      > > result.Tables["MyNewTable "].Columns.Add("C ompanyName"); //Add a single
      > > column to the DataTable
      > > result.Tables["MyNewTable "].Columns.Add("F ormType"); //Add a single
      > > column
      > > result.Tables["MyNewTable "].Columns.Add("C IK"); //Add a single
      > > column
      > > result.Tables["MyNewTable "].Columns.Add("D ateFiled"); //Add a single
      > > column
      > > result.Tables["MyNewTable "].Columns.Add("S ECWebAddress"); //Add a single
      > > column
      > >
      > > string AllData1 = sr.ReadToEnd(); //Read the rest of the
      > > data in the file.
      > > string[] rows = AllData1.Split( "\n".ToCharArra y()); //Split off[/color]
      > each[color=green]
      > > row at the Line Feed
      > >
      > > foreach(string r in rows) //Now add each row to the
      > > DataSet
      > > {
      > > string delimStr1 = "\t";
      > > string[] items = r.Split(delimSt r1.ToCharArray( )); //Split the row at the
      > > delimiter
      > > result.Tables["MyNewTable "].Rows.Add(items ); //Add the item
      > > }
      > >
      > > for (int i = 1; i <= 11; i++) //Remove first[/color]
      > 8[color=green]
      > > rows from the DataTable/DataSet
      > > {
      > > result.Tables["MyNewTable "].Rows.RemoveAt( 0);
      > > }
      > >
      > > dataGrid1.SetDa taBinding(resul t, "MyNewTable "); //Binds DataGrid to
      > > DataSet,display ing datatable
      > >
      > > SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter( ); //Configure a
      > > data adapter
      > >
      > > //Configure the SQL connection
      > > SqlConnection sqlConnection1 = new SqlConnection(" workstation[/color]
      > id=AMD;packet[color=green]
      > > size=4096;" +
      > > "integrated security=SSPI;d ata source=AMD;pers ist security
      > > info=False;init ial catalog=SEC_XBR L_10");
      > >
      > > //Generate the 'INSERT' command
      > > SqlCommand sqlInsertComman d1 = new SqlCommand("INS ERT INTO
      > > SEC_Index_01(Co mpanyName, FormType, CIK, " +
      > > "DateFiled, SECWebAddress) VALUES (@CompanyName, @FormType, @CIK,
      > > @DateFiled, @FileName); SELECT ID, " +
      > > "CompanyNam e, FormType, CIK, DateFiled, SECWebAddress FROM SEC_Index_01
      > > WHERE (ID = @@IDENTITY)", sqlConnection1) ;
      > >
      > > //Add the parameters
      > > sqlInsertComman d1.Parameters.A dd("@CompanyNam e",
      > > System.Data.Sql DbType.VarChar, 8000, "CompanyNam e");
      > > sqlInsertComman d1.Parameters.A dd("@FormType" ,[/color]
      > System.Data.Sql DbType.VarChar,[color=green]
      > > 8000, "FormType") ;
      > > sqlInsertComman d1.Parameters.A dd("@CIK", System.Data.Sql DbType.VarChar,
      > > 8000, "CIK");
      > > sqlInsertComman d1.Parameters.A dd("@DateFiled" ,
      > > System.Data.Sql DbType.VarChar, 8000, "DateFiled" );
      > > sqlInsertComman d1.Parameters.A dd("@FileName" ,[/color]
      > System.Data.Sql DbType.VarChar,[color=green]
      > > 8000, "SECWebAddress" );
      > >
      > >
      > > sqlDataAdapter1 .InsertCommand = sqlInsertComman d1; //Set the[/color]
      > insert[color=green]
      > > command
      > >
      > > sqlDataAdapter1 .Update(result, "SEC_Index_01") ; //Perform the
      > > update
      > >
      > >[/color]
      >
      >
      >[/color]

      Comment

      Working...