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 connection string?

525


differences between ADO and ADO.NET

568


What are the disadvantages of using datalist?

505


Why edit is not possible in repeater?

519


how you will deal result set? How do you sort a dataset?If a dataset contains 100 rows, how to fetch rows between 5 and 15 only?

579






What is ole word?

527


What is bubbled event?

535


How do you update a dataset in ado.net and how do you update database through dataset?

481


How to perform sorting on a table in ADO.NET?

553


Which database is the ado.net?

496


What is ole data type?

503


How to add a check box or a dropdown list to a column in a datagrid?

533


What is difference between sqldatareader and sqldataadapter?

483


What is ado.net components?

502


What is a datareader object?

577