Click here to Skip to main content
15,885,939 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
give me some suggestion to store data in database using c#.net.
i m blank...
get array and display it through some textbox or gridview
Posted
Updated 31-Mar-11 21:19pm
v2

1 solution

To enter data into database, assuming you have two fields
"myField1":nvarchar(50)
"myField2":datetime
And are using MS Sql:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myField1, myField2) " +
                                           "VALUES (@MF1, @MF2)", con))
        {
        com.Parameters.AddWithValue("@MF1", "Test");
        com.Parameters.AddWithValue("@MF2", DateTime.Now);
        com.ExecuteNonQuery();
        }
    }

To read it out, a TextBox is not the best choice, somethign that displays rows fo data would be better: try a DataGridView:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    DataSet ds = new DataSet();
    SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM myTable", con);
    adp.Fill(ds);
    myDataGridView.DataSource = ds;
    }
 
Share this answer
 
Comments
Albin Abel 18-Mar-11 6:02am    
Good effort. my 5
Espen Harlinn 18-Mar-11 6:34am    
Nice and instructive - 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900