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
Explain the differences between oledb sql server, oledbdotnet provider?
What is a string variable?
How do I delete a row from a DataTable?
What is dataset object? Explain the various objects in dataset.
What is the use of SqlCommandBuilder?
Describe ado.net object model in detail.
Which is faster ado.net or linq?
Which one of the following objects is a high-level abstraction of the connection and command objects in ado.net?
Define Execute Reader?
Define different execute methods of ADO.NET command object ?
What is the difference between the clone() and copy() methods of the dataset class?
What are all the different methods under sqlcommand?
What is the use of Dataview?
How to connect and retrieve data from database using dataset
What are all the different authentication techniques used to connect to MS SQL Server?