Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use adapter when returned by the following function

How to use adapter when returned by the following function

Scheduled Pinned Locked Moved C#
tutorialannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CPK_2011
    wrote on last edited by
    #1

    public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
    {
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the commands.
    adapter.SelectCommand = new SqlCommand(
        "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);
    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);
    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
    
    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", 
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName", 
        SqlDbType.VarChar, 40, "CompanyName");
    
    adapter.UpdateCommand.Parameters.Add("@CustomerID", 
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName", 
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", 
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
    
    adapter.DeleteCommand.Parameters.Add("@CustomerID", 
        SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
    
    return adapter;
    

    }

    The above function returns adapter of SqlDataAdapter type. Could anyone suggest on how to use it for InsertCommand, UpdateCommand and DeleteCommand in some other place.

    N 1 Reply Last reply
    0
    • C CPK_2011

      public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
      {
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

      // Create the commands.
      adapter.SelectCommand = new SqlCommand(
          "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
      adapter.InsertCommand = new SqlCommand(
          "INSERT INTO Customers (CustomerID, CompanyName) " +
          "VALUES (@CustomerID, @CompanyName)", connection);
      adapter.UpdateCommand = new SqlCommand(
          "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
          "WHERE CustomerID = @oldCustomerID", connection);
      adapter.DeleteCommand = new SqlCommand(
          "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
      
      // Create the parameters.
      adapter.InsertCommand.Parameters.Add("@CustomerID", 
          SqlDbType.Char, 5, "CustomerID");
      adapter.InsertCommand.Parameters.Add("@CompanyName", 
          SqlDbType.VarChar, 40, "CompanyName");
      
      adapter.UpdateCommand.Parameters.Add("@CustomerID", 
          SqlDbType.Char, 5, "CustomerID");
      adapter.UpdateCommand.Parameters.Add("@CompanyName", 
          SqlDbType.VarChar, 40, "CompanyName");
      adapter.UpdateCommand.Parameters.Add("@oldCustomerID", 
          SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
      
      adapter.DeleteCommand.Parameters.Add("@CustomerID", 
          SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
      
      return adapter;
      

      }

      The above function returns adapter of SqlDataAdapter type. Could anyone suggest on how to use it for InsertCommand, UpdateCommand and DeleteCommand in some other place.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      praveen.code999 wrote:

      how to use it for InsertCommand, UpdateCommand and DeleteCommand in some other place.

      Do you want an option to specify the command which has to be created instead of creating all three?

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups