Using SqlDataAdapter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RN1

    Using SqlDataAdapter

    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(strS QL, sqlConn)

    sqlConn.Open()
    sqlCmd.ExecuteN onQuery()
    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?
  • =?Utf-8?B?TWFuaXNo?=

    #2
    RE: Using SqlDataAdapter

    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("UPDA TE Categories SET ");
    sb.Append("Cate goryName=@sCate goryName WHERE CategoryID=@nCa tegoryID");
    SqlConnection conn = this.getConnect ionString();
    SqlCommand cmd = new SqlCommand(sb.T oString(), conn);
    SqlParameter p1 = new SqlParameter("@ sCategoryName", SqlDbType.VarCh ar, 30);
    p1.Direction = ParameterDirect ion.Input;
    p1.Value = ((TextBox)(e.It em.Cells[1].Controls[0])).Text;
    cmd.Parameters. Add(p1);

    SqlParameter p2 = new SqlParameter("@ nCategoryID", SqlDbType.Int);
    p2.Direction = ParameterDirect ion.Input;
    p2.Value = C1WebGrid1.Data Keys[e.Item.ItemInde x];
    cmd.Parameters. Add(p2);

    conn.Open();
    cmd.ExecuteNonQ uery();
    conn.Close();

    Regards,
    Manish
    MESCIUS USA Inc. has JavaScript and .NET UI controls, datagrids, reporting solutions, spreadsheets, document APIs and more to take your applications further.



    "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(strS QL, sqlConn)
    >
    sqlConn.Open()
    sqlCmd.ExecuteN onQuery()
    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?
    >

    Comment

    Working...