C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






how to use a SqlDataAdapter to get data from a database into a DataSet.


Posted Date: 08 May 2008    Resource Type: Code Snippets    Category: ADO.NET

Posted By: komaladevi       Member Level: Gold
Rating:     Points: 5




Dim sConnection As String = "server=(local);uid=sa;pwd=password;database=yourDatabase"

Dim objDataAdapter As New SqlDataAdapter("Select * From tableName", sConnection)
Dim dsResult As New DataSet("Result")

If Not IsNothing(objDataAdapter) Then
' Fill data into dataset
objDataAdapter.Fill(dsResult)

objDataAdapter.Dispose()
End If

' Test by bind data into datagridview control
Me.DataGridView1.DataSource = dsResult.Tables(0)






Responses

Author: Kamal    08 May 2008Member Level: Gold   Points : 2
hi,

this is very easy but give solution for DataReader to Dataset.


Author: vivek kushwaha    08 May 2008Member Level: Silver   Points : 2
you can modified it like below.

Dim sConnection As String = "server=(local);uid=sa;pwd=password;database=yourDatabase"
Dim objDataAdapter As New SqlDataAdapter("Select * From tableName", sConnection)
Dim dsResult As New DataSet

'Fill data into dataset
objDataAdapter.Fill(dsResult)
'' no requirement to dispose in .net there is automatic finalize() exist.

Me.DataGridView1.DataSource = dsResult.Tables(0)
me.datagridview1.databind()


Author: vivek kushwaha    08 May 2008Member Level: Silver   Points : 2

ConvertDataReaderToDataSet: ///
/// Converts a SqlDataReader to a DataSet
///
/// SqlDataReader to convert.
///
/// DataSet filled with the contents of the reader.

///

public static DataSet convertDataReaderToDataSet(SqlDataReader reader)
{
DataSet dataSet = new DataSet();
do
{
// Create new data table

DataTable schemaTable = reader.GetSchemaTable();
DataTable dataTable = new DataTable();

if ( schemaTable != null )
{
// A query returning records was executed

for ( int i = 0; i < schemaTable.Rows.Count; i++ )
{
DataRow dataRow = schemaTable.Rows[ i ];
// Create a column name that is unique in the data table
string columnName = ( string )dataRow[ "ColumnName" ]; //+ "";
// Add the column definition to the data table
DataColumn column = new DataColumn( columnName, ( Type )dataRow[ "DataType" ] );
dataTable.Columns.Add( column );
}

dataSet.Tables.Add( dataTable );

// Fill the data table we just created

while ( reader.Read() )
{
DataRow dataRow = dataTable.NewRow();

for ( int i = 0; i < reader.FieldCount; i++ )
dataRow[ i ] = reader.GetValue( i );

dataTable.Rows.Add( dataRow );
}
}
else
{
// No records were returned

DataColumn column = new DataColumn("RowsAffected");
dataTable.Columns.Add(column);
dataSet.Tables.Add( dataTable );
DataRow dataRow = dataTable.NewRow();
dataRow[0] = reader.RecordsAffected;
dataTable.Rows.Add( dataRow );
}
}
while ( reader.NextResult() );
return dataSet;
}




Author: Neelesh Parkhi    09 May 2008Member Level: Silver   Points : 2
Hi solution is:
// using SqlConnection object
SqlConection Conn=new SqlConnection(connectionstring);
String Sql="Select * from Master[Table Name]";
SqlDataAdapter Adapter=new SqlDataAdapter(Sql,Conn);
// Create DataSet
DataSet ds=new DataSet()
Adapter.Fill(ds);
//Storing value in grid
GridView1.DataSource=ds.Table[0];
GridView1.DataBind();


Author: venkat kamal    09 May 2008Member Level: Gold   Points : 2
Hi this code is one which every one knows.....try to give some thing new as no one knows.......


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Build up a DataSet
Previous Resource: Simple program to add/delete data to database
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use