Sunday, February 12, 2012

Accessing Queries in sqlDataAdapter

Hello Dears;

I have an SqlDataAdapter which contains may queries. One query contians 3 query parameters as follows:

SELECT SUM(Amount) AS Total
FROM Boxes
WHERE EntryDate BETWEEN @.date1 AND @.date2 ANDArea=@.Area

the query cannot be ran unless the queries parameters were provided. I have to access this query in code to add the parameters from controls. Is it possible to access it or use sqlDataSource instead?

Thanks alot

Here is one example I grabed from tutorial with a little modifications for your reference:

Sub GetAuthors_Click(SenderAs Object, EAs EventArgs)

Dim DSAs DataSet
Dim MyConnectionAs SqlConnection
Dim MyCommandAs SqlDataAdapter

Dim strSelectCommandAsString = "select * from Authors where state = @.State"

MyConnection =New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("PubString").ConnectionString
MyCommand =New SqlDataAdapter(strSelectCommand, MyConnection)

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@.State", SqlDbType.NVarChar, 2))
MyCommand.SelectCommand.Parameters("@.State").Value = someValue 'you need send in

DS =new DataSet()
MyCommand.Fill(DS, "Authors")

Mygv.DataSource=DS.Tables("Authors").DefaultView
Mygv.DataBind()
End Sub

You can add all your parameters to your SqlDataAdapter from your code.

|||Thanks Limno

No comments:

Post a Comment