Using SqlDataAdapter

Discussion in 'ASP .Net' started by RN1, Mar 17, 2008.

  1. RN1

    RN1 Guest

    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?
    RN1, Mar 17, 2008
    #1
    1. Advertising

  2. RN1

    Manish Guest

    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?
    >
    Manish, Mar 17, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zhaoJian
    Replies:
    0
    Views:
    1,002
    zhaoJian
    Nov 6, 2003
  2. Dan

    TimeOut on SqlDataAdapter.Fill

    Dan, Apr 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,791
  3. Jerry Higgins
    Replies:
    6
    Views:
    616
    Jerry Higgins
    Apr 21, 2004
  4. Ashish
    Replies:
    3
    Views:
    3,986
    Naveen K Kohli
    Sep 7, 2004
  5. Andy G

    Using sqlDataAdapter to fill Dataset

    Andy G, Jan 31, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    208
    Elton Wang
    Jan 31, 2005
Loading...

Share This Page