What are the steps in connecting to database?

Answer Posted / sireesha

C#:
System.Data.SqlClient;
In the method {}...
Step 1: Create a connection:
SqlConnection con = new SqlConnection();
Con.ConnectionString = “DataSource =Server Name;
Initial Catalog = Database Name; Integrated Security=True”;
Step 2: Create a Command:
SqlCommand cmd = new SqlCommangd();
Step 3: Bind the Command to Connection:
cmd.Connection = con;
Step 4: Initialise the Command with SQL Statement to execute
EX:-
String Name = txtName.Text;
decimal Salary = decimal.parse(txtsalary.Text);
cmd.CommandText = "insert into Emp
(EmpName,EmpSalary) Value(' "+name+" ',"+salary")";
Step 5: Open the Connection
con.Open();
Step 6: Execute the Command
Like we have different execute command statements
like ExecuteNonQuery(),ExecuteScalar(),ExecuteReader(). Use
as per the requirement of data.
Step 7: Close the connection
con.Close();

Is This Answer Correct ?    8 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the advantages of ado.net?

683


What is the difference between typed and untyped dataset?

707


Explain the DataAdapter.Update() and DataSetAcceptChanges() methods.

746


Which is faster entity framework or ado.net?

701


Explain the role of data provider in ado.net?

751


What is datasource in ado.net?

820


What is oledb driver?

706


What is the difference between SqlCommand and SqlCommandBuilder?

791


Describe connection object in ado.net

755


What is the difference between OLEDB Provider and SqlClient?

723


How to create data relations?

740


What is commandbuilder in ado.net?

725


Which database is the ado.net sql connection object designed for?

818


What are the ado.net objects?

698


What are the key events of sqlconnection class?

849