Understanding database access using ADO.NET in c# |
|||
DATABASE ACCESS WITH ADO NETC# Tutorials |
|||
|
|
|||
|
|
|||
DATABASE ACCESS WITH ADO NETLearn Tutorials |
|
Understanding database access using ADO.NET in c#
Example code for Database access with ADO NET: basic database access with ADO.NET //using System.Data.SqlClient - optimized for SQL server //using System.Data.OleDb - optimized for MSaccess, Excel, dBase //using System.Data.Odbc - for odbc datasources set up in console //using System.Data.OracleClient - for Oracle databases private void accessDB() { //create connection obj SqlConnection connObj = new SqlConnection(); connObj.ConnectionString = @"server=www.yourdbservernamehere.com;Database=FirstProtect;uid=;pwd="; //create SqlDataReader object to receive the data from the database SqlDataReader dr; //create SqlCommand object and set its properties below SqlCommand cmd = new SqlCommand(); //set the sql statement as the CommandText property of the SqlCommand obj cmd.CommandText="SELECT fname,lname FROM testtable"; //set the Connection property of the SqlCommand obj cmd.Connection = connObj; //open the connection connObj.Open(); //call the ExecuteReader method of the Command obj to execute the command dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //dump the dataset returned while (dr.Read()) { Console.WriteLine(dr.GetString(0)+" "+dr.GetString(1)); } //close the Connection obj connObj.Close(); } |
Home
CSHARP Arrays in c sharp C sharp array objects C sharp separators context menu Compare dates csharp datetime Context menu c sharp Context menus csharp example Controls c sharp dynamically Database access c sharp Database access with ADO NET DataGrid c sharp Datagrid dataset csharp NET Datagrid Styles data grid Datasets listview dropdown selectbox Dialogbox csharp dialog box Drag and drop csharp Listview combo box csharp Looping through hashtable NET NET collections tutorial NET crawler safe url One row from dataset Output datetime csharp Popup form csharp Try catch c sharp |
||||||
![]() | |||||||