0

In every ASP.NET application I have written, I would make number of request to the database before outputting the information onto the web page.

For example:

var DataTable1 = GetDataTable("Select * From Customers");
var DataTable2 = GetDataTable("Select * From Products");
var DataTable3 = GetDataTable("Select * From Orders");

As far as I'm aware, the code above would make 3 separate trips to the database and would do them one after the other.

Is there anyway I can gather together my parameterized SQL statements and make only 1 trip to the database server?

2
  • A better way to describe this would be "multiple database requests in one trip", not "parallel database requests". "Parallel" means they all happen literally at the same time, which doesn't seem to be what you're seeking; there are ways to do that too, though that makes little sense for a web page. Commented Mar 5, 2009 at 14:02
  • Thanks for the heads up. Your right. I've changed the title :) Commented Mar 5, 2009 at 18:25

4 Answers 4

6
var SqlString = "SELECT * FROM Customers; SELECT * FROM Products; SELECT * FROM ORDERS;");
var ds = GetDataSet(SqlString);
var DataTable1 = ds.Tables(0);
var DataTable2 = ds.Tables(1);
var DataTable3 = ds.Tables(2);
Sign up to request clarification or add additional context in comments.

Comments

1

My solution:

SqlConnection con = new SqlConnection("Server=CLASS-III-WKS10\\SQLEXPRESS;Initial 
Catalog=wind;Integrated Security=True");
int[] id=new int[9];
int i = 0;
page_load()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select *from windmill", con);
    SqlDataReader rd = cmd.ExecuteReader();
    while (rd.Read())
    {
        id[i] = rd.GetInt32(9);
        i++;
        //MessageBox.Show(id.ToString());  
    }
    rd.close();
    SqlCommand cmd1 = new SqlCommand("Update windmill set State='Stopped',PmState='Not Available'where Id=0", con);
    cmd1.ExecuteNonQuery(); 
}

Comments

0

Use semi-colons to separate SQL statements and reader.NextResult to get each set. Your code will end up looking something like this.

    Using con As New SqlConnection
        Using cmd As New SqlCommand("Select * From Customers; Select * From Products; Select * From Orders")
            Using reader = cmd.ExecuteReader
                Dim dt1 As DataTable
                dt1.Load(reader)

                reader.NextResult()

                Dim dt2 As DataTable
                dt2.Load(reader)

                reader.NextResult()


                Dim dt3 As DataTable
                dt3.Load(reader)

            End Using
        End Using
    End Using

Comments

-1

Create a Thread class and place a method in that class which accept string query as input: then create a Thread object and start the object.

Check this out http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx

1 Comment

That would still make 3 trips to the database except it would do them at the same time. Not really the solution I was looking for.

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.