Using SqlDataAdapter

  • Thread starter Thread starter RN1
  • Start date Start date
R

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?
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top