2010-10-12 85 views
1

當我加載我的頁面時,使用下面的代碼填充我的中繼器。在頁面加載時只連接數據庫一次

 Dim conn As Data.SqlClient.SqlConnection 
     Dim Comm As Data.SqlClient.SqlCommand 
     Dim reader As Data.SqlClient.SqlDataReader 

     'conn = New Data.SqlClient.SqlConnection("Server=localhost\sqlexpress; " & _ 
     '"Database=MyDB; Integrated Security=true") 
     conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString) 

     Comm = New Data.SqlClient.SqlCommand(_ 
     ("HomePage"), conn) 

     Comm.CommandType = CommandType.StoredProcedure 
     Comm.Parameters.AddWithValue("@currentState", "Florida") 

     ' Open the connection 
     conn.Open() 
     ' Execute the category command 
     reader = Comm.ExecuteReader() 

     ' Bind the reader to the repeater....... 
     blogRepeater.DataSource = reader 

     blogRepeater.DataBind() 

     ' Close the reader 
     reader.Close() 
     ' Close the connection 
     conn.Close() 

    End Try 

現在我想調用另一個存儲過程(在同一時間),這樣我就可以填充某些文本字段(也頁面加載)。但是,我怎麼能這樣做,以便我只打一次電話給我的數據庫以獲得更好的性能?

C#示例也將工作,如果你不知道VB.NET

回答

4

只要不關閉連接和脫火另一個存儲過程。之後關閉連接。所以你會昏暗另一個SQL命令並執行它。

喜歡的東西:

Dim Comm2 As Data.SqlClient.SqlCommand 
Dim reader2 as Data.SqlClient.SqlDataReader 

Comm2.CommandType = CommandType.StoredProcedure 
Comm2.Paramaters.AddWithValue("@whateverValue", "Whatever") 

那就請你打開剛過連接

reader2 = Comm2.ExecuteReader() 

那麼你會發現reader2有你想要什麼,而是你用兩個相同的連接。

+0

在性能vs代碼可維護性競賽中這是最好的選擇 – NotMe 2010-10-12 17:20:37

+0

感謝Man,其實很簡單!謝謝! – Etienne 2010-10-12 17:28:30

+0

@Etienne - 沒問題,很高興我能幫上忙 – 2010-10-12 17:43:38