Click here to Skip to main content
6,306,412 members and growing! (19,490 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

SQLDataAdapter without using SQLCommandBuilder

By vinoth1979

This article speaks about how to use SQLDataAdapter and its update method without using SQLCommandBuilder
C#, Windows, .NET, Visual Studio, DBA, Dev
Posted:28 Jan 2004
Views:106,359
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
28 votes for this article.
Popularity: 3.56 Rating: 2.46 out of 5
12 votes, 42.9%
1
3 votes, 10.7%
2
2 votes, 7.1%
3
7 votes, 25.0%
4
4 votes, 14.3%
5

Introduction

This article talks about how to use SQLDataAdapter and its update method without using SQLCommandBuilder.

The code

The following code creates a database connection and then the data adapter for the connection; and then filling the data set using the adapter and binding the data grid with the data set. Now creating four command objects namely SelectCommand, InsertCommand, UpdateCommand, DeleteCommand for the data adapter.

There comes the important thing, closely look at the parameter added with the every command object. The add method of SQLParameterCollection accepts 4 parameter values. They are,

Parameter name, db type, size, and column name. so for every change in row state of data set, the update command is going to use the row to build the corresponding query and then updates the data source.

//Connecting database

con = new SqlConnection(
  "Data Source=mysource;Initial Catalog=mydbname;uid=sa");
//create sql adapter for the "emp" table

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from emp", con);
//create dataset instance

DataSet    dSet = new DataSet();
//fill the dataset

sqlDa.Fill(dSet, "emp");
//bind the data grid with the data set

dataGrid1.DataSource=dSet.Tables["emp"];

//build select command

SqlCommand selCmd = new SqlCommand("select * from emp",con);
sqlDa.SelectCommand=selCmd;

//build insert command

SqlCommand insCmd = new SqlCommand(
  "insert into emp (Name, Age) values(@Name, @Age)",con);
insCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");
insCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
sqlDa.InsertCommand = insCmd;

//build update command

SqlCommand upCmd = new SqlCommand(
  "update emp set Name=@Name, Age=@Age where No=@No",con);
upCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");
upCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
upCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.UpdateCommand = upCmd;

//build delete command

SqlCommand delCmd = new SqlCommand(
  "delete from emp where No=@No",con);
delCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.DeleteCommand = delCmd;

//now update the data adapter with dataset.

sqlDa.Update(dSet,"emp");

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

vinoth1979


Member

Occupation: Web Developer
Location: India India

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralUpdate Failed............... Pinmemberrparbat2:04 18 Mar '09  
QuestionSQl command builder PinmemberMember 38728200:36 11 Oct '08  
GeneralOleDbCommandBuilder (Please help me) PinmemberHasanVaez5:30 1 Feb '08  
AnswerRe: OleDbCommandBuilder (Please help me) PinmemberMember 38728200:34 11 Oct '08  
GeneralTo those who say : boring & nothing new Pinmemberplatus20001:56 15 Jan '06  
GeneralRe: To those who say : boring & nothing new Pinmemberharryjo3:25 9 Feb '06  
GeneralCan't Update The DataBase Pinmemberboaz2583:09 23 Jun '05  
GeneralRe: Can't Update The DataBase PinmemberHasanVaez5:37 1 Feb '08  
GeneralWhat am I missing ? PinsussBarneaGal22:04 28 Jan '04  
GeneralRe: What am I missing ? Pinmembervinoth197922:27 28 Jan '04  
GeneralRe: What am I missing ? PinmemberMember 359322321:23 8 Jun '08  
Generalmsdn article on this topic Pinmembervinoth19790:23 29 Jan '04  
GeneralBoring! Pinmemberdog_spawn9:42 29 Jan '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jan 2004
Editor: Nishant Sivakumar
Copyright 2004 by vinoth1979
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project