You are on page 1of 4

SQLDataAdapter without using SQLCommandBuilder - CodeProject

7,397,942 members and growing! (19,251 online)

Email Password Sign in Join



Remember me? Lost password?

Home Articles Questions & Answers Learning Zones Jobs Features Help! The Lounge

Sponsored Links
Article Browse Code Stats Revisions 16 2.49 / 5, 31 votes
$4.95/month ASP.NET Web Hosting
ASP.NET 4 Web Hosting with MS
» Database » Database » General SQL & MySQL...
First Posted 28 Jan 2004 www.winhost.com

SQLDataAdapter without using Views


Bookmarked
123,154
43 times
Resco MobileApp Studio

SQLCommandBuilder
Resco MobileApp Studio is a
Licence Microsoft Visual...
C#, Windows, .NET, Visual- www.resco.net
By vinoth1979 | 28 Jan 2004 Studio, DBA, Dev,
Intermediate ActiveReports by GrapeCity
This article speaks about how to use SQLDataAdapter and its ActiveReports by GrapeCity is the...
update method without using SQLCommandBuilder www.datadynamics.com

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

The code See Also...


DatabaseAccessor
The following code creates a database connection and then the data adapter for the connection; and
A simple base class that
then filling the data set using the adapter and binding the data grid with the data set. Now creating
encapsulates basic...
four command objects namely SelectCommand , InsertCommand , UpdateCommand ,
DeleteCommand for the data adapter. From Custom Authentication to
ASP.NET Forms Authentication
There comes the important thing, closely look at the parameter added with the every command object. Illustrates replacing custom
The add method of SQLParameterCollection accepts 4 parameter values. They are, authentication...
AJAX AutoComplete/AutoSuggest
Parameter name, db type, size, and column name. so for every change in row state of data set, the
TextBox
update command is going to use the row to build the corresponding query and then updates the data
An article on AJAX AutoSuggest
source.
control
Collapse | Copy Code Building a Web Site with
//Connecting database Membership and User Login
con = new SqlConnection( An article about building a Web Site
"Data Source=mysource;Initial Catalog=mydbname;uid=sa");
//create sql adapter for the "emp" table with...
SqlDataAdapter sqlDa = new SqlDataAdapter("select * from emp", con);
//create dataset instance Working with SQL Server Logins
DataSet dSet = new DataSet();
//fill the dataset Learn the details of how to work
sqlDa.Fill(dSet, "emp"); with SQL...
//bind the data grid with the data set
dataGrid1.DataSource=dSet.Tables["emp"];
//build select command
SqlCommand selCmd = new SqlCommand("select * from emp",con); Announcements
sqlDa.SelectCommand=selCmd;
//build insert command Want a Robot? Enter Our
SqlCommand insCmd = new SqlCommand( Hardware Comp!
"insert into emp (Name, Age) values(@Name, @Age)",con);
insCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name"); Got an Azure app? Win an
insCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age"); Xbox & Kinect
sqlDa.InsertCommand = insCmd;
Monthly Competition
//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"); The Daily Insider
upCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.UpdateCommand = upCmd;
30 free programming books
//build delete command Daily News: Signup now.
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");

http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx[10/16/2010 10:56:48 AM]


SQLDataAdapter without using SQLCommandBuilder - CodeProject

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

Web Developer

India

Member

Sign Up to vote for this article


Article Top

Comments and Discussions

You must Sign In to use this message board.


FAQ Search

Noise Tolerance Medium


Medium Layout ExpandPosts
Expand Posts& & Replies
Replies Per page 10
10 Update

Msgs 1 to 10 of 16 (Total in Forum: 16) (Refresh) First Prev Next

My vote of 5 MohammadAli SJ 5:38 1 Oct '10

very clearly and useful

Sign In · View Thread · PermaLink

a problem with SqlDataAdapter and Update MohammadAli SJ 22:51 30 Sep '10

hi there
I read your article from here:http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx"
and I used this code:

string connection = "Server=NIMIS\\TEST;database=Projects;integrated


security=true";
string query="UPDATE fest SET nwem='6' WHERE num='20'";
SqlConnection mycon = new SqlConnection(connection);
SqlDataAdapter mydaap = new SqlDataAdapter("select * from fest", mycon);
SqlCommand up = new SqlCommand(query, mycon);
DataSet myset = new DataSet();
mydaap.Fill(myset, "fest");
mydaap.UpdateCommand = up;
mydaap.Update(myset, "fest");

but my table did not updated...


please help me to find what is the problem problem...
thank you

Sign In · View Thread · PermaLink

My vote of 1 Member 1639673 6:10 3 Sep '09

the code is incomplete

Sign In · View Thread · PermaLink

http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx[10/16/2010 10:56:48 AM]


SQLDataAdapter without using SQLCommandBuilder - CodeProject

Update Failed............... rparbat 2:04 18 Mar '09

SqlConnection con = new SqlConnection(str);


SqlCommand cmd = new SqlCommand("select * from Employee", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds,"emp");
// GridView1.DataSource=ds;
//GridView1.DataBind();

//build update command


con.Open();
SqlCommand upCmd = new SqlCommand("update employee set Salary=@Salary where eno=@eno", con);
upCmd.Parameters.Add("@Salary", SqlDbType.Int,6,"45000");
upCmd.Parameters.Add("@eno", SqlDbType.NChar,10,"105");
adp.UpdateCommand = upCmd;
adp.Update(ds.Tables[0]);
adp.Fill(ds.Tables[0]);
GridView1.DataSource = ds;
GridView1.DataBind();

No updates found
please see why ???
i have checked everything

Sign In · View Thread · PermaLink

SQl command builder Member 3872820 0:36 11 Oct '08

I m new in dot net...


So please some one help me to use Sql command builder for add,update and delete sql server table.....
i want to use only sql command builder...

Sign In · View Thread · PermaLink 5.00/5 (1 vote)

OleDbCommandBuilder (Please help me) HasanVaez 5:30 1 Feb '08

Dear friends,

I am going to update, delete and insert my data to Access data base via data grid:

Public Class Form1


Dim strDataBasePath As String = "\\hasan-pc\share\"
Dim strConDB As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataBasePath & "m.mdb;"
Dim CON As New OleDb.OleDbConnection(strConDB)
Dim CMD As New OleDb.OleDbCommand("SELECT * FROM table1", CON)
Dim ADP As New OleDb.OleDbDataAdapter(CMD)
Dim CMDB As New OleDb.OleDbCommandBuilder(ADP)
Dim DTABLE As New DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
CON.Open()
ADP.Fill(DTABLE)
G.DataSource = DTABLE
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button2.Click
ADP.InsertCommand = CMDB.GetInsertCommand
ADP.DeleteCommand = CMDB.GetDeleteCommand
ADP.UpdateCommand = CMDB.GetUpdateCommand
ADP.Update(DTABLE)
End Sub
End Class

I succeed if insert a record, but I got Failed when I am going to delete or update a record.
I got this error:
"Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not
return any key column information."

or

"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not
return any key column information."

Please help me what should I do.

http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx[10/16/2010 10:56:48 AM]


SQLDataAdapter without using SQLCommandBuilder - CodeProject

hasanvaez@gmail.com

hasan

Sign In · View Thread · PermaLink 1.00/5 (1 vote)

Re: OleDbCommandBuilder (Please help me) Member 3872820 0:34 11 Oct '08

Make sure tat in your select query there is one unique key

Sign In · View Thread · PermaLink

To those who say : boring & nothing new platus2000 1:56 15 Jan '06

you might be surprised, but some of the programmers who come to websites such as codeproject.com are
really beginners, and to them sometimes the basics of some trivial (to you) issues need explaining, in order
to advance to the more complex topics...

However a 'BEGINNERS' warning in the title would be handy.

Sign In · View Thread · PermaLink 4.00/5 (3 votes)

Re: To those who say : boring & nothing new harryjo 3:25 9 Feb '06

That's correct, I'm such a "beginner" and I find it very helpfull.


I wass looking for code to update my sql DB with the SqlCommandBuilder but
find this article. It still doesn't resolved my problem, maybe you can help me .

I try to update an Sql Table with SqlCommandBuilder,


But I don't make the changes in the datagrid, but in fields who are binded to the datagrid. Is there a
solution to do this ?

Sign In · View Thread · PermaLink

Can't Update The DataBase boaz258 3:09 23 Jun '05

why
sqlDa.Update(sDet,"emp") doesn't update the database ?
I did every thing like this artical.

Sign In · View Thread · PermaLink

Last Visit: 19:00 31 Dec '99 Last Update: 18:56 15 Oct '10 1 2 Next »

General News Question Answer Joke Rant Admin

PermaLink | Privacy | Terms of Use Copyright 2004 by vinoth1979


Last Updated: 28 Jan 2004 Everything else Copyright © CodeProject, 1999- 2010
Web24 | Advertise on the Code Project

http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx[10/16/2010 10:56:48 AM]