none
use if...else statements in ado.net RRS feed

  • Question

  • hey guys

    i want to use T_SQL if....else statement in ado.net (string that contains T_SQL if...else)

    can anyone help me ?

    • Moved by CoolDadTx Thursday, March 2, 2017 8:00 PM ADO.NET related
    Thursday, March 2, 2017 7:48 PM

Answers

  • What issue are you having? You can do ad hoc queries in a SqlCommand. The only major command that doesn't work is GO. You have to execute a separate command each time GO would appear in a normal query.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, March 2, 2017 8:00 PM
  • Hi arman_gorjipoor,

    Here is a simple demo for your reference.

    string connstr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AdoDotNetDemo;Integrated Security=True";
    
                string strSQL = @"declare @count int,
                                    @contactname varchar(50)
                                    set @contactname = 'xxx';
                                    select @count = count(1) from Customer where ContactName = @contactname;
                                    if(@count > 0)
                                       update Customer set ContactName = 'bbb' where ContactName = @contactname;
                                    else
                                       insert into Customer (ContactName) values (@contactname);";
    
                using (var conn = new SqlConnection(connstr))
                {
                    using (var cmd = new SqlCommand(strSQL,conn))
                    {
                        conn.Open();
                        int rows = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 3, 2017 6:05 AM
    Moderator

All replies

  • What issue are you having? You can do ad hoc queries in a SqlCommand. The only major command that doesn't work is GO. You have to execute a separate command each time GO would appear in a normal query.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, March 2, 2017 8:00 PM
  • Hi arman_gorjipoor,

    Here is a simple demo for your reference.

    string connstr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AdoDotNetDemo;Integrated Security=True";
    
                string strSQL = @"declare @count int,
                                    @contactname varchar(50)
                                    set @contactname = 'xxx';
                                    select @count = count(1) from Customer where ContactName = @contactname;
                                    if(@count > 0)
                                       update Customer set ContactName = 'bbb' where ContactName = @contactname;
                                    else
                                       insert into Customer (ContactName) values (@contactname);";
    
                using (var conn = new SqlConnection(connstr))
                {
                    using (var cmd = new SqlCommand(strSQL,conn))
                    {
                        conn.Open();
                        int rows = cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 3, 2017 6:05 AM
    Moderator