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 is the difference between Command and CommandBuilder object?

806


Which architecture does Datasets follow?

815


What are the ado.net objects?

714


Describe the disconnected architecture of ADO.NET’s data access model.

787


How can we save all data from dataset?

746


What are basic methods of dataadapter?

728


Explain the different row versions available in table?

767


What is the difference between Datareader and Dataset?

782


Which method is used to sort the data in ADO.Net?

1765


Which method in OLEDBAdapter is used to populate dataset with records?

793


Define isolation?

759


What is executequery?

706


What are the benefits of ADO.NET?

872


What are the steps to connect to a database?

766


How would you connect to a database by using .NET?

726