What is sequence of code in retrieving data from database ?
Answers were Sorted based on User's Feedback
if you use sql server
SqlConnection con = new SqlConnection();
con.ConnectionString ="Data Source=servername;Initial
Catalog=databasename;";
con.Open();
String sql;
sql ="select * from tbl_tablename";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader reader;
reader = cmd.ExecuteReader();
while(reader.Read)
{
Console.WriteLine("Field1:"+read.GetString(0));
Console.WriteLine("Field1:"+read.GetString(1));
Console.WriteLine("Field1:"+read.GetValue(2));
}
con.Close();
}
Is This Answer Correct ? | 9 Yes | 4 No |
Answer / manoj batra
If using DataSet :
1) Create a connection object
2) Create a DataAdapter object
3) Associate your query and connection with DataAdapter
4) Create DataSet object
5) Fill the dataset with desired result using Fill method of
DataAdapter.
6) start retriving the data into your container from dataset.
Note : DataAdapter automatically Open and Close the connection.
If using DataReader:
1) Create connection object
2) Oen connection
3) Create Command object
4) Create DataReader object
5) Associate your Sql command with the command object
6) Use ExecuteDataReader method inside the loop (The
selective Sql query may return more than one Row)
Note : DataReader is Readonly and ForwardOnly operation.
7) Return result in control(s).
8) Close the connection
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / payal
//In this answer there is sqlserver
/*i write this code in vb.net with windows applications and
use sql server provider*/
//first need namespace
system.data.sqlclient
//need connection,command,connection string
dim sqlcmd as sqlcommand
dim sqlcon as sqlconnection
dom da as sqldataadaptor
dim ds as dataset
dim constr as sting="user
password=sss;datasource=sds;initial catalog=fgf"
/*connection string give all details about database
connection initial catalog means data base name and data
source is sqlserver name,user id and password also*/
//open connection and pass connection string
sqlcon=new sqlconnection(constr)
sqlcon.open()
//create new object of command with new keyword
sqlcmd=new sqlcommand("select * from tablename",sqlcon)
/*data adapter is mediater between the data base and data
set because dataset is work in disconneted mode.*/
da=new sqldataadaptor(sqlcmd)
/*data adapter fetch query to data base and get answer and
fil the dataset using fill*/
da.fill(ds)
/*there is datagird which i use for display data on
form.data from dataset i use this bind to datagrid*/
datagird.datasource=ds.tables("table name")
Is This Answer Correct ? | 4 Yes | 2 No |
Which parameter of ConnectionString is used to specify the name of the database?
Some important instruction regarding ADO.NET connection string ?
What is data relation?
What is data view and variable view?
What is sequence of code in retrieving data from database ?
3 Answers Accenture, BirlaSoft,
what is bubbled event can u pls explain
How to call the SQL commands asynchronously in ADO.NET version 2.0
What is the default view of Data Grid?
What is meant by executenonquery?
what is connection pooling ?
What are the advantages and disadvantages of using datalist?
What is the difference between ADO and ADO.Net?