Monday 8 April 2013

Insert data from textbox to mdf-database. (c#)


Insert data from textbox to mdf-database. (c#)


protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
    {
        SqlCommand CmdSql = new SqlCommand("INSERT INTO [Movies] (ID, Name, Genre, Length, Year, Description) VALUES (@ID, @Name, @Genre, @Length, @Year, @Description)", conn);
        conn.Open();
        CmdSql.Parameters.AddWithValue("@ID", TextBox1.Text);
        CmdSql.Parameters.AddWithValue("@Name", TextBox2.Text);
        CmdSql.Parameters.AddWithValue("@Genre", TextBox3.Text);
        CmdSql.Parameters.AddWithValue("@Length", TextBox4.Text);
        CmdSql.Parameters.AddWithValue("@Year", TextBox5.Text);
        CmdSql.Parameters.AddWithValue("@Description", TextBox6.Text);
        CmdSql.ExecuteNonQuery();
        conn.Close();
    }
}

No comments:

Post a Comment