473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using SqlDataAdapter

RN1
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?
Mar 17 '08 #1
1 1376
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
"RN1" wrote:
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?
Mar 17 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope...
6
by: goodstart123 | last post by:
For example, the following code works fine without SqlConnection. Any Answer to the question? string connectionString = "server=YourServer; uid=sa; pwd=YourPassword;...
3
by: Khurram | last post by:
Hi, I am trying to use Crystal Reports with ASP.NET. I created a dataset and tried to display it in the report but nothing came up. I did not get an exception either. I know that the dataset is...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
7
by: Spencer H. Prue | last post by:
Hello!, I have a using system.web.sessionstate directive one the top of each of two web pages. On the first page I have the dataadapter, dataconnection, and dataset (also in the component tray)....
0
by: koonda | last post by:
Hi guys, I posted a message before and I got some help from the forum. I have a database in SQL Server and I wanted to create a web interface in C#. I had 4 Listbox controls and a Textbox and two...
12
by: Simon | last post by:
Hi all, I'm having a baffling problem with a windows service that I'm working on. Basically, I am using a typed dataset to insert a large number of rows into an SQL Server 2005 database. But...
1
by: TonyJ | last post by:
Hello! I trying to update a table using sql server but no update will be done. I don't get any error at all. Here is an extract from the code. Does this seems to be right.
0
MrMancunian
by: MrMancunian | last post by:
How to create a database connection without using wizards Introduction I've seen a lot of questions on the net about getting data from, and saving data to databases. Here's a little insight how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.