none
fill table with 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

    Friday, May 8, 2009 3:03 PM

Answers

  • i found it:

    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();
    }

    Saturday, May 9, 2009 5:33 PM

All replies

  • pleeease someone help me.i am in trouble and don`t know what should i do
    Friday, May 8, 2009 4:44 PM
  • could i use insert command? please help me
    Saturday, May 9, 2009 6:48 AM
  • 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
  • i found it:

    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();
    }

    Saturday, May 9, 2009 5:33 PM