TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Load DataTable From SqlDataReader
Arjun Dhilod
Aug 20
2016
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System.Data;
using
System.Configuration;
using
System.Data.SqlClient;
protected
void
ConvertDataReaderToDataTable(
object
sender, EventArgs e) {
string
constr = ConfigurationManager.ConnectionStrings[
"constr"
].ConnectionString;
using
(SqlConnection con =
new
SqlConnection(constr)) {
using
(SqlCommand cmd =
new
SqlCommand(
"SELECT * FROM Customers"
)) {
cmd.Connection = con;
con.Open();
using
(SqlDataReader sdr = cmd.ExecuteReader()) {
//Create a new DataTable.
DataTable dtCustomers =
new
DataTable(
"Customers"
);
//Load DataReader into the DataTable.
dtCustomers.Load(sdr);
}
con.Close();
}
}
}
Load DataTable
SqlDataReader
C#