Thursday 8 December 2011

Code for Creating Connection with SQLSERVER 2008 using Csharpe

//Connection – used to connect to the data source
SqlConnection use = new SqlConnection(" Data Source=FAISALN;Initial Catalog=test;Integrated Security=True ");

 //DataAdapter use to populate the dataset  with data from the DataSource
            SqlDataAdapter da = new SqlDataAdapter();

//Command– used to execute a command against the data source and retrieve a DataReader or DataSet, or to execute an              INSERT, UPDATE, or DELETE command against the data source

 da.InsertCommand = new SqlCommand("insert into Username values (@Username, @Password)", use);
da.InsertCommand.Parameters.Add("@username", SqlDbType.NVarChar).Value = textBox1.Text;
da.InsertCommand.Parameters.Add("@password", SqlDbType.NVarChar).Value = textBox2.Text;
          
use.Open();
da.InsertCommand.ExecuteNonQuery();
use.Close();
//Clear the textboxes
            textBox1.Text = "";
            textBox2.Text = "";

No comments:

Post a Comment