Using SQL statements in C# >NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kprabhudev
    New Member
    • Apr 2007
    • 1

    Using SQL statements in C# >NET

    I have tried to use the "DELETE" statement to delete a row from the table, which satisfies part_number not equal to EA*. But this part is not working.


    OleDbDataAdapte r MyAdapterBasic = new OleDbDataAdapte r("SELECT part_id, part_number, description FROM parts;DELETE FROM parts WHERE part_number LIKE 'EA%'", MyConnection);


    I have used two SQL statements seperated by ; symbol.
    Is there any best way to do this.

    Error: An unhandled exception of type 'System.Data.Ol eDb.OleDbExcept ion' occurred in system.data.dll

    Please help...
  • bhar
    Banned
    New Member
    • Apr 2006
    • 37

    #2
    Hi,

    usage of DataAdapter in C# 2005

    //Instantiate DataAdapter.
    The below statements instantiate the DataAdapter objects. We need to instantiate the DataAdapters one each for the data we retrieve and data we update.
    [code=vb]
    myAccountAdapte r = new SqlDataAdapter( str_Account_Sel ect, myConnection);
    myAllAccountAda pter = new SqlDataAdapter( str_Allaccount_ Select, myConnection);

    //Setting the DataAdapter command properties.

    The DataAdapter class has four properties:

    1. SelectCommand
    2. InsertCommand
    3. UpdateCommand
    4. DeleteCommand

    Each of the property perform a specific operation on a database. Listed below are the four properties and their purpose.

    1. SelectCommand
    This read-write property returns or sets the SQL statement that will be used when rows are selected/retrieved from the data source.

    2. InsertCommand.
    This read-write property returns or sets the SQL statement that will be used when rows are inserted in to the data source.

    3. UpdateCommand
    This read-write property returns or sets the SQL statement that will be used when rows are updated in the data source.

    4. DeleteCommand.
    This read-write property returns or sets the SQL statement that will be used when rows are deleted from the data source.

    When the DataAdapter needs to retrieve or send data to and from the database, it uses Command objects which must be specified. We specify the command objects for selecting, updating, and deleting rows in the database. This is done by creating command objects, and then assigning them to the appropriate DataAdapter property: SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand are shown below.

    //Set DataAdapter Command properties
    myAccountAdapte r.SelectCommand = comAccountSelec t;
    myAllAccountAda pter.SelectComm and = comAllAccountSe lect;
    myAccountAdapte r.InsertCommand = comAccountInser t;
    myAccountAdapte r.DeleteCommand = comAccountDelet e;
    myAccountAdapte r.UpdateCommand = comAccountUpdat e;

    //Add Delete Command parameters.
    comAccountDelet e.Parameters.Ad d("@id", SqlDbType.Char, 6, "AccountCod e");
    [/code]
    [Link Removed]
    Last edited by pbmods; Oct 22 '07, 12:01 PM. Reason: Added CODE tags.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Bhar,

      You are a great resource to the forum and have been extremely helpful in providing comprehensive and detailed answers to people's questions; especially in concerns to .NET. Your help is greatly appreciated.

      Please refrain from including links to advertisements as this is against forum rules.
      Please review the FAQ Forum for posting rules.
      This link contains information concerning were it is acceptable to post these types of links.

      Thanks,

      MODERATOR

      Comment

      Working...