0

I am building an application in which I will producing some reports based off the results of some SQL queries executed against a number of different databases and servers. Since I am unable to create stored procedures on each server, I have my SQL scripts saved locally, load them into my C# application and execute them against each server using ADO.NET. All of the SQL scripts are selects that return tables, however, some of them are more complicated than others and involve multiple selects into table variables that get joined on, like the super basic example below.

My question is, using ADO.NET, is it possible to assign a string of multiple SQL queries that ultimately only returns a single data table to a SqlCommand object - e.g. the two SELECT statements below comprising my complete script? Or would I have to create a transaction and execute each individual query separately as its own command?

-- First Select
SELECT *
INTO #temp
FROM Table1;

--Second Select
SELECT *
FROM Table1
JOIN #temp
ON Table1.Id = #temp.Id;

Additionally, some of my scripts have comments embedded in them like the rudimentary example above - would these need to be removed or are they effectively ignored within the string? This seems to be working with single queries, in other words the "--This is a comment" is effectively ignored.

    private void button1_Click(object sender, EventArgs e)
    {
        string ConnectionString = "Server=server1;Database=test1;Trusted_Connection=True";
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {

            SqlCommand cmd = new SqlCommand("--This is a comment \n SELECT TOP 10 * FROM dbo.Tablw1;");
            DataTable dt = new DataTable();
            SqlDataAdapter sqlAdapt = new SqlDataAdapter(cmd.CommandText.ToString(), conn);
            sqlAdapt.Fill(dt);
            MessageBox.Show(dt.Rows.Count.ToString());
        }
    }
3
  • 1
    I believe you can, but why haven't you just tried it? Why ask a question that you can easily answer yourself through a simple test? Commented Jun 30, 2016 at 12:53
  • @ScottHannen, I think you have mis-read the question. His multi-statement command will only produce one result set. Commented Jun 30, 2016 at 13:29
  • You're right. I did. Commented Jun 30, 2016 at 13:29

3 Answers 3

1

Yes, that is absolutely fine. Comments are ignored. It should work fine. The only thing to watch is the scopin of temporary tables - if you are used to working with stored procedures, the scope is temporary (they are removed when the stored procedure ends); with direct commands: it isn't - they are connection-specific but survive between multiple operations. If that is a problem, take a look at "table variables".

Note: technically this is up to the backend provider; assuming you are using a standard database engine, you'll be OK. If you are using something exotic, then it might be a genuine question. For example, it might not work on "Bob's homemade OneNote ADO.NET provider".

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can positively do it. You can play with different types of collections, or with string Builder for passing queries even you can put the string variable and assign the query to it.

While the loop is running put in temp table or CTE, its totally depends on you to choose the approach. and add the data to datatable.

So if you want the entire data to be inserted or Updated or deleted then you can go for transaction,it won't be any issue.

Comments

0

I don't use ado.net, I use Entity Framework but I think this is more a SQL question than an ADO.NET question; Forgive me if I'm wrong. Provided you are selecting from Table1 in both queries I think you should use this query instead.

select * from Table1 tbl1 join Table1 tbl2 on tbl1.id = tbl2.id

Actually I really don't ever see a reason you would have to move things into temp tables with options like Common Table Expressions available to you. look up CTEs if you don't already know about them

https://www.simple-talk.com/sql/t-sql-programming/sql-server-cte-basics/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.