Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Using SqlDataAdapter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 17 Mar 2008 01:52 GMT
When using the SqlCommand object, records can be inserted/updated/
deleted in the underlying data source (i.e. the SQL Server database
table) directly by using code like this:

--------------------------------------------------------------------------------
Dim strSQL As String
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection
strSQL = "INSERT INTO MyTable (Col1, Col2) VALUES........."
sqlConn = New SqlConnection("........")
sqlCmd = New SqlCommand(strSQL, sqlConn)

sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlConn.Close()
--------------------------------------------------------------------------------

But if the SqlDataAdapter object is used to insert/update/delete
records in the data source, is it ALWAYS necessary to first make the
necessary changes in the DataSet for the data source to reflect the
changes?
Manish - 17 Mar 2008 05:49 GMT
Does your SqlDataAdapter have the required SqlCommands it needs (e.g.,
select, insert, update, delete)?

if yes then you can update using code below:

StringBuilder sb = new StringBuilder("");
sb.Append("UPDATE Categories SET ");
sb.Append("CategoryName=@sCategoryName WHERE CategoryID=@nCategoryID");
SqlConnection conn = this.getConnectionString();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
SqlParameter p1 = new SqlParameter("@sCategoryName", SqlDbType.VarChar, 30);
p1.Direction = ParameterDirection.Input;
p1.Value = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;
cmd.Parameters.Add(p1);

SqlParameter p2 = new SqlParameter("@nCategoryID", SqlDbType.Int);
p2.Direction = ParameterDirection.Input;
p2.Value = C1WebGrid1.DataKeys[e.Item.ItemIndex];
cmd.Parameters.Add(p2);

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

Regards,
Manish
www.ComponentOne.com

> When using the SqlCommand object, records can be inserted/updated/
> deleted in the underlying data source (i.e. the SQL Server database
[quoted text clipped - 17 lines]
> necessary changes in the DataSet for the data source to reflect the
> changes?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.