SqlCommand in C#

Hi Expert,

Can I use one cmd and change type to use it again and how to do it?


using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("SP_UPDATE", con))

                    {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Test", TestString1);
                   cmd.ExecuteNonQuery();

                   // after update done, select record from updated table
                    cmd.CommandType = CommandType.Text;
                   String  str1="Select * from table"



Thanks in advance
LVL 1
dshi15Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
You are on the right track.

// after update done, select record from updated table 
cmd.CommandType = CommandType.Text;
string str1 = "Select * from table";

SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);

Open in new window


The DataTable dt will now have the records.


Cheers
hongjun
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hongjunCommented:
To iterate, see below example:

string name;

// on all table's rows
foreach (DataRow row in dt.Rows)
{
    //assuming one of the column is "Name"
    name = row["Name"].ToString();
}

Open in new window

0
Bob LearnedCommented:
If you have 2 SQL statements that you are trying to execute, then I would suggest that you have  methods that you can pass in the command text or stored procedure name to execute, along with parameters.

public static void ExecuteNonQuery(string commandText, CommandType commandType, params SqlParameter[] parameterList)
{
   using (var connection = new SqlConnection(connectionString))
   {
       connection.Open();
       using (var command = new SqlCommand(commandText, connection))
       {
           command.CommandType = commandType;

           command.Parameters.AddRange(parameterList);
           command.ExecuteNonQuery();
       }
   }
}

public static DataTable ExecuteDataTable(string commandText, CommandType commandType, params SqlParameter[] parameterList)
{
   using (var connection = new SqlConnection(connectionString))
   {
       connection.Open();
       using (var command = new SqlCommand(commandText, connection))
       {
           command.CommandType = commandType;

           command.Parameters.AddRange(parameterList);
           using (var reader = command.ExecuteReader())
           {
                 var table = new DataTable();
                 table.Load(reader);
                 return table;
           }
       }
   }
}

Open in new window


Usage:
DataLayer.ExecuteNonQuery("SP_UPDATE", CommandType.StoredProcedure, new SqlParameter("@Test", TestString1)

var table = DataLayer.ExecuteDataTable("Select * From Table", CommandType.CommandText);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.