2014-03-25 22 views
-2

我创建了这个查询,但我不知道如何将它绑定到sqldatasource,所以我可以创建一个datalist,有人可以帮我吗?SQL查询到SQL数据源

Dim cn As SqlConnection = New SqlConnection() 
    Dim cmd As SqlCommand = New SqlCommand() 
    Dim dr As SqlDataReader 

    If Session("Email") <> "" Then 
     cn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 
     cmd.Connection = cn 
     cmd.CommandText = "Select Name From [Games] Where Platform = @Platform AND Genre = @Genre AND Age >= @Age" 

     Dim Platform As New SqlParameter("@Platform", SqlDbType.VarChar, 20) 
     Platform.Value = ddlPlatform.SelectedItem.ToString() 
     cmd.Parameters.Add(Platform) 

     Dim Genre As New SqlParameter("@Platform", SqlDbType.VarChar, 20) 
     Genre.Value = ddlgenre.SelectedItem.ToString() 
     cmd.Parameters.Add(Genre) 

     Dim Age As New SqlParameter("@Age", SqlDbType.Int) 
     Age.Value = txtAge.Text.ToString() 
     cmd.Parameters.Add(Age) 

     'Open the connection to the database 
     cn.Open() 
     ' Execute the sql. 
     dr = cmd.ExecuteReader()` 
+0

你能说出你的问题显然 –

+0

对不起,我需要在DataList控件来显示这个查询的结果,我不知道怎么回事,我只用过的SqlDataSource。 – Alexandria

+0

看看这个链接http://msdn.microsoft.com/en-us/library/aa719635(v=vs.71).aspx –

回答

0

感谢Vignesh我已经能够回答这个问题。

 Dim cn As SqlConnection = New SqlConnection() 
    Dim cmd As SqlCommand = New SqlCommand() 
    cn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 
    cmd.Connection = cn 
    cmd.CommandText = "Select Name, Platform, Genre, Age_Range From [Games] Where (Platform = @Platform) and (Genre = @Genre) and (Age_Range <= @Age)" 

    Dim Platform As New SqlParameter("@Platform", SqlDbType.VarChar, 20) 
    Platform.Value = ddlPlatform.SelectedItem.ToString() 
    cmd.Parameters.Add(Platform) 

    Dim Genre As New SqlParameter("@Genre", SqlDbType.VarChar, 20) 
    Genre.Value = ddlgenre.SelectedItem.ToString() 
    cmd.Parameters.Add(Genre) 

    Dim Age As New SqlParameter("@Age", SqlDbType.VarChar, 20) 
    Age.Value = txtAge.Text 
    cmd.Parameters.Add(Age) 

    cn.Open() 
    Dim ds As SqlDataReader 
    ds = cmd.ExecuteReader() 
    MyDatalist.DataSource = ds 
    MyDatalist.DataBind() 
    cn.Close()