How do I call this function?

I'm very sorry for asking, I'm used in designing Datasets from the DataSet Designer then I tried to shift to making codes for them manually, I got this example from MSDN. How will I use or call the adapter from this function

If I want to use the SelectCommand or the other commands, what should I write in my code

for example from Form1_Load, I want to use the SelectCommand.

Thanks in advance.

public static SqlDataAdapter CreateCustomerAdapter(

SqlConnection connection)

{

SqlDataAdapter adapter = new SqlDataAdapter();

// Create the SelectCommand.

SqlCommand command = new SqlCommand("SELECT * FROM Customers " +

"WHERE Country = @Country AND City = @City", connection);

// Add the parameters for the SelectCommand.

command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);

command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

adapter.SelectCommand = command;

// Create the InsertCommand.

command = new SqlCommand(

"INSERT INTO Customers (CustomerID, CompanyName) " +

"VALUES (@CustomerID, @CompanyName)", connection);

// Add the parameters for the InsertCommand.

command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");

command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

adapter.InsertCommand = command;

// Create the UpdateCommand.

command = new SqlCommand(

"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +

"WHERE CustomerID = @oldCustomerID", connection);

// Add the parameters for the UpdateCommand.

command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");

command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

SqlParameter parameter = command.Parameters.Add(

"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");

parameter.SourceVersion = DataRowVersion.Original;

adapter.UpdateCommand = command;

// Create the DeleteCommand.

command = new SqlCommand(

"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Add the parameters for the DeleteCommand.

parameter = command.Parameters.Add(

"@CustomerID", SqlDbType.NChar, 5, "CustomerID");

parameter.SourceVersion = DataRowVersion.Original;

adapter.DeleteCommand = command;

return adapter;

}




Answer this question

How do I call this function?

  • amr osama

    Thanks for you reply! For a delete command you can do something like this:


    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet result = new DataSet();


    string query = "DELETE * FROM Table1";

    using(SqlConnection dbconn = new SqlConnection("myconnectionstring"))
    using(SqlCommand command = dbconn.CreateCommand())
    {
    command.CommandText = query;

    try
    {

    dbconn.Open();

    int effected = command.ExecuteNonQuery();
    MessageBox.Show( this, "Delete command executed succesfully and effected " + effected + " rows." );
    }
    catch(SqlException caught)
    {
    MessageBox.Show( this, "Error while executing delete command.\r\n\r\nDetails:\r\n" + caught.ToString() );
    }
    finally
    {
    dbconn.Close();
    }
    }




  • MarcHoeppnerNeoGeo

    Thanks! U

  • Carl B.

    how about if i'd like to use the .Delete command

  • rkpetersen

    You are welcome!


  • Richard Tkachuk

    As PJ. van de Sande says right, you should use the SqlDataAdapter.Fill() method.

    The code for the function you supplied should be like:

    DataSet result = new DataSet();

    SqlDataAdapter adapter = CreateCustomerAdapter(connection);

    adapter.Fill(result);



  • Owen T. Soroke

    In the Load event of the Form class you put some code like this:

    In the Load event of the Form class you put some code like this:


    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet result = new DataSet();


    string query = "SELECT * FROM Table1";

    using(SqlConnection dbconn = new SqlConnection("myconnectionstring"))
    using(SqlCommand command = dbconn.CreateCommand())
    {
        command.CommandText = query;
      
        adapter.SelectCommand = command;
        adapter.Fill( result );
    }


    // TODO: Use the filled 'result' here.

     


  • How do I call this function?