Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone.

I searched on the net, but couldn't find a nice example including basic database operations(insert, update, delete..)

Does anyone has a nice link or an example to show how to make such operations using SqlServer?

My best regards...
Posted

 
Share this answer
 
v2
Comments
Un_NaMeD 12-Nov-11 15:03pm    
Hey thatraja.
Thnx for quick answer.
That example is pretty cool, but a little bit complicated.
I need something very simple that only shows add, delete and update ops.
My best regards...
thatraja 12-Nov-11 15:09pm    
Check my updated answer
This is how to insert the the value in the table. Here I'm inserting the values in the registration table on the button event.
private void Register_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=.\\sqlexpress; initial catalog=yourDatabasename; integrated security=true");
try
                {
                    con.Open();                    
                   
                    SqlCommand  cmd = new SqlCommand("insert into Registration values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + textBox4.Text + "')", con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Congratulation! Successfully Register!"); 
                                        
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Got:" + ex.Message);
                }
}


Here how you can update the record

C#
private void Chpawrd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=.\\sqlexpress; initial catalog=yourDatabasename; integrated security=true");
try
               {
                   con.Open();
                   SqlCommand cmd = new SqlCommand("update registration set password='" + textBox3.Text + "' where username='" + textBox1.Text + "'", con);
                   cmd.ExecuteNonQuery();
                   MessageBox.Show("Password changed Successfully");
               }
               catch (Exception ex)
               {
                   MessageBox.Show("Error:-" + ex.Message);
               }
               con.Close();

Here I'm updating the existed password. Where the username is from the texbox1 and the related password from this username will be changed.

And the delete option you can use as same the update with the where condition. I think this will help you.
 
Share this answer
 
 
Share this answer
 

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