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
What is connection string?
differences between ADO and ADO.NET
What are the disadvantages of using datalist?
Why edit is not possible in repeater?
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?
What is ole word?
What is bubbled event?
How do you update a dataset in ado.net and how do you update database through dataset?
How to perform sorting on a table in ADO.NET?
Which database is the ado.net?
What is ole data type?
How to add a check box or a dropdown list to a column in a datagrid?
What is difference between sqldatareader and sqldataadapter?
What is ado.net components?
What is a datareader object?