繁体   English   中英

SQL 查询到 SQL 数据源

[英]SQL Query to SQL DataSource

我已经创建了这个查询,但我不知道如何将它绑定到 sqldatasource 以便我可以创建一个数据列表,有人可以帮助我吗?

    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()`

感谢 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM