Friday, March 24, 2017

c# insert data into msserver easily


In this  lesson we know about how to make a software using c# language.

First open visual studio File->New->Project->Windows Froms Application-> Ok.

c# insert data int mssql











Then we use toolbox for design the input field and button.

How use Visual studio toolbox











Visual studio from design



















After design complete now we have to create a database.Open the sqlserver which version we use.Now we  create a new database by following this picture.

Sql server database create

sql server table create











Then we create a table on this database.













After UI and database design complete ,Press double click on the form button  and write this code or copy it from here and paste it.




















//This is connection code with the sql server. Data source is sqlserver name,Initial catalog is the database we create on sql server.
SqlConnection con = new SqlConnection(@"Data Source=USER;Initial Catalog=testo;Integrated Security=True");

private void button1_Click(object sender, EventArgs e)
        {
// Open the database connection in this line and create sql command.
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into yy                  values('"+textBox1.Text+"','"+textBox4.Text+"')";

//Then here the sql query execute.

            cmd.ExecuteNonQuery();

//close the connection
          
            con.Close();

//clear the table field.
            textBox1.Clear();
          
            textBox2.Clear();

//For Display message after saved.

            MessageBox.Show("Saved Successful");
        }

 We have to add library name  (sql client) as we can see on the image.

Add data client on code










Then we save the code.Now click on start for execute the code. Now insert the data.

play visual studio project











Now we saved the data by click insert button.A message popup data insert successfully.

C# insert Sales succesful



No comments:

Post a Comment