locked
insert into table for an array RRS feed

  • Question

  • hi.i have a program that gets data from a loop one by one with one second delay and i want to store data in my database,the data are going first to my array then to database(i need it like this),so how can i add it to my database?my table has 2 column and i want to store data as nvarchar data in one of the columns named "numbers".the other column called "id" and is my primary key.

    for every test(every click on my button)i want to begin inserting data into table and every time in new row and for the row of the next "id" column. 

    my code just for get data :

    private void button1_Click(object sender, EventArgs e)

    {

    rrayList a1 = new ArrayList();for (Double i = 0; i < 10; i++)

    {

    a1.Add(i);

    Double[] d1 = (Double[])a1.ToArray(typeof(Double));

     

    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString);String s2 =

    string.Join(",", s1);Thread.Sleep(1000);

    }

    }

    the output is :0,1,2,3,4,5,6,7,8,9

     what should i use for filling "numbers" with the array(with s2 output).pleeeeease help me.

    thanks

    Saturday, May 9, 2009 3:17 PM

Answers

  • The other responses are if you want one row per loop (10 rows total).  If I understand you correctly, you want just one row total, with all 10 values in it?

    If so, just modify your loop code from this:
    for (Double i = 0; i < 10; i++) 
    {
    Thread.Sleep(1000); 
    a1.Add(i);
    Double[] d1 = (Double[])a1.ToArray(typeof(Double)); 
    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString); 
    
    s2 = string.Join(",", s1); 
    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')"; 
    
    SqlCommand insertinto = new SqlCommand(insert,  myConnection); 
    insertinto.ExecuteNonQuery();
    } 
    To this:

    for (Double i = 0; i < 10; i++) 
    {
    Thread.Sleep(1000); 
    a1.Add(i);
    Double[] d1 = (Double[])a1.ToArray(typeof(Double)); 
    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString); 
    s2 = string.Join(",", s1); 
    } 
    
    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')"; 
    SqlCommand insertinto = new SqlCommand(insert, myConnection); 
    insertinto.ExecuteNonQuery();
    

    Michael Asher
    • Marked as answer by Bin-ze Zhao Tuesday, May 12, 2009 9:36 AM
    Saturday, May 9, 2009 4:42 PM
  • What is the need for creating arrays?

    You can achieve the same using the following code

    String s = String.Empty;

    for (Double{ i = 0; i < 10; i++)
    {
    Thread.Sleep(1000);
    s = s + i.ToString

    if(s!=9)
    s += ",";

    string insert = "Insert Into Table1([Decible]) Values ('" + s + "')";
    SqlCommand insertinto = new SqlCommand(insert, myConnection);
    insertinto.ExecuteNonQuery();
    }

    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    Saturday, May 9, 2009 5:44 PM

All replies

  • Your code does'nt seem complete.

    Can you post again.


    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    Saturday, May 9, 2009 3:28 PM
  • hi.i wrote my program but there is a problem after executing and it is that i want the whole output (0,1,2,3,4,5,6,7,8,9) set and store in just one row of my "Decible" column but it stores (0) in first row,(0,1) in the second row,(0,1,2) in the third ...........and (0,1,2,3,4,5,6,7,8,9) in the tenth row!!! i know it is because of my for loop but i don`t know how to solve it.pleeeeeeeeeeeeease anser me.thanks

    my whole program:

     

    private void button1_Click(object sender, EventArgs e)

    {

     

    string myConnectionString = "Data Source=SOFTNET\\SQLEXPRESS;Initial Catalog=D2;Integrated Security=True";

     

    SqlConnection myConnection = new SqlConnection(myConnectionString);

    myConnection.Open();

     

    string s2;

     

    ArrayList a1 = new ArrayList();

     

    for (Double i = 0; i < 10; i++)

    {

     

    Thread.Sleep(1000);

    a1.Add(i);

     

    Double[] d1 = (Double[])a1.ToArray(typeof(Double));

     

    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString);

    s2 =

    string.Join(",", s1);

     

    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')";

     

    SqlCommand insertinto = new SqlCommand(insert, myConnection);

    insertinto.ExecuteNonQuery();

    }

    myConnection.Close();

     

     

    }

    Saturday, May 9, 2009 3:50 PM
  • for (Double{ i = 0; i < 10; i++)
    {
    Thread.Sleep(1000);
    string insert = "Insert Into Table1([Decible]) Values ('" + i.ToString() + "')";
    SqlCommand insertinto = new SqlCommand(insert, myConnection);
    insertinto.ExecuteNonQuery();
    }


    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    Saturday, May 9, 2009 3:58 PM
  • string sql = "Insert Into Table1(MyColumn) Values (@MyValue)";
    using (SqlCommand cmd = new SqlCommand(sql, conn)
    {
        SqlParameter myValue = new SqlParameter("@MyValue", SqlDbType.Decimal);
        cmd.Parameters.Add(myValue);
        conn.Open();
        cmd.Prepare();
        for (Int32 i = 0; i < 10; i++)
        {
             myValue.Value = i;
             cmd.ExecuteNonQuery();
        }
        conn.Close();
       
    }



    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Saturday, May 9, 2009 4:34 PM
  • By the way if you have an array of a specific type, use List<T> rather then an ArrayList

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Saturday, May 9, 2009 4:38 PM
  • The other responses are if you want one row per loop (10 rows total).  If I understand you correctly, you want just one row total, with all 10 values in it?

    If so, just modify your loop code from this:
    for (Double i = 0; i < 10; i++) 
    {
    Thread.Sleep(1000); 
    a1.Add(i);
    Double[] d1 = (Double[])a1.ToArray(typeof(Double)); 
    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString); 
    
    s2 = string.Join(",", s1); 
    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')"; 
    
    SqlCommand insertinto = new SqlCommand(insert,  myConnection); 
    insertinto.ExecuteNonQuery();
    } 
    To this:

    for (Double i = 0; i < 10; i++) 
    {
    Thread.Sleep(1000); 
    a1.Add(i);
    Double[] d1 = (Double[])a1.ToArray(typeof(Double)); 
    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString); 
    s2 = string.Join(",", s1); 
    } 
    
    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')"; 
    SqlCommand insertinto = new SqlCommand(insert, myConnection); 
    insertinto.ExecuteNonQuery();
    

    Michael Asher
    • Marked as answer by Bin-ze Zhao Tuesday, May 12, 2009 9:36 AM
    Saturday, May 9, 2009 4:42 PM
  • actually i have done it before,it sould not be like this because in this way s2 is local and could not be used in the whole program so i expand the for loop to the whole program ,it worked but it stores (0) in first row,(0,1) in the second row,(0,1,2) in the third ...........and (0,1,2,3,4,5,6,7,8,9) in the tenth row!!! so i found the answer;i should use s2 as a public string,now the program is like yhis and it works:

    public

     

    partial class Form6 : Form

    {

     

    public string s2;

     

    public Form6()

    {

    InitializeComponent();

    }


     

    private void button1_Click(object sender, EventArgs e)

    {

     

    string myConnectionString = "Data Source=SOFTNET\\SQLEXPRESS;Initial Catalog=D2;Integrated Security=True";

     

    SqlConnection myConnection = new SqlConnection(myConnectionString);

    myConnection.Open();

     

    ArrayList a1 = new ArrayList();

     

    for (Double i = 0; i < 20; i++)

    {

     

    Thread.Sleep(1000);

    a1.Add(i);

     

    Double[] d1 = (Double[])a1.ToArray(typeof(Double));

     

    String[] s1 = Array.ConvertAll<Double, string>(d1, Convert.ToString);

    s2 =

    string.Join(",", s1);

    }

     

    string insert = "Insert Into Table1([Decible]) Values ('" + s2 + "')";

     

    SqlCommand insertinto = new SqlCommand(insert, myConnection);

    insertinto.ExecuteNonQuery();

    • Marked as answer by Bin-ze Zhao Tuesday, May 12, 2009 9:29 AM
    • Unmarked as answer by Bin-ze Zhao Tuesday, May 12, 2009 9:37 AM
    Saturday, May 9, 2009 5:31 PM
  • What is the need for creating arrays?

    You can achieve the same using the following code

    String s = String.Empty;

    for (Double{ i = 0; i < 10; i++)
    {
    Thread.Sleep(1000);
    s = s + i.ToString

    if(s!=9)
    s += ",";

    string insert = "Insert Into Table1([Decible]) Values ('" + s + "')";
    SqlCommand insertinto = new SqlCommand(insert, myConnection);
    insertinto.ExecuteNonQuery();
    }

    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    Saturday, May 9, 2009 5:44 PM