.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Update Database Table using DataSet

Posted By:Praveen       Posted Date: December 02, 2008    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  
 

Below is the code to update DataBase tableĀ using DataSet with the help of CommandBuilder Object. Its Simple. SqlDataAdapter.Update() method is been used to Update DataBase in disconnected architecture.

private void UpdateDataBaseWithDataSet()

{

SqlConnection conn = new SqlConnection("Server=ServerName; Database=DBName; User ID=yourDBUserID; Password=yourDBPassword;");

try

{

conn.Open(); //Open Connection

SqlDataAdapter dataAdapter = new SqlDataAdapter();

SqlCommand sqlComnd = new SqlCommand("select * from TestTable", conn);

dataAdapter.SelectCommand = sqlComnd;

SqlCommandBuilder comndBuilder = new SqlCommandBuilder(dataAdapter);

DataSet dataSet = new DataSet();

dataAdapter.Fill(dataSet);

DataRow dr;

dr = dataSet.Tables[0].Rows[0]; //Extract the row youwant to update

dr["UserName"] = "Any Other Name"; // Set the Column UserName name to any other value

dataAdapter.Update(dataSet);

}

catch (Exception ex)

{

throw ex;

}

finally

{

conn.Close(); //Close SQL Connection

}

}

Hope this will helpfull to all the developer who want to update DataBase table using DataSet.

Happy Coding!!!


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend