none
insert an array into a table 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 5:27 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:35 PM