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 |
What is sql connection in ado.net?
what is data access layer?
What are the difference between readonly vs. Const?
Describe the command object and its method.
Whate are different types of Commands available with DataAdapter ?
What is Serialization in .NET? what are the types of Serialization?
What is the default Timeout for SqlCommand.CommandTimeout property?
Where do you store connection string ?
What is difference between executenonquery and executequery?
What is bubbled event can you please explain?
What is the advantage of ado.net?
Give few examples of datareader that is used in different dataproviders.