0

我正在尝试从另一列查询特定列文本的数据。

基本上,我有一个包含 SupplierID 和 Country 列的供应商数据库。

例如,我已经有了该特定行的 SupplierID,它是 14。我想根据 14 值获取 Country 列的文本值。

我通过以下代码(列表框)获得的供应商 ID:

<asp:ListBox ID="SupplierListBox" runat="server" 
            DataSourceID="SupplierCompanyDataSource" DataTextField="Company" 
            DataValueField="SupplierID" Width="315px" 
            Height="80px" 
            onselectedindexchanged="SupplierListBox_SelectedIndexChanged" 
            AutoPostBack="True"></asp:ListBox>

代码:

        string SupplierListvalue = SupplierListBox.SelectedItem.Value; //SupplierListvalue retrieves the SupplierID value

        SqlDataReader rdr = null;
        SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select Country from SupplierDB", conn);
        cmd.Connection = conn;

        conn.Open();
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            TextBox1.Text = rdr["Country"].ToString();
            MessageBox.Show("Connection Successful");
            MessageBox.Show(rdr.ToString());
        }

        conn.Close();
4

3 回答 3

2

好吧,目前还不清楚主要问题是什么,所以我将向您展示一个工作示例,它Country使用 ADO.NET 从数据库中选择列,使用参数来避免 SQL 注入和using-statement 以确保所有非托管资源作为连接得到处置(关闭)。

string sql = @"
            SELECT Country
            FROM dbo.Supplier
            WHERE SupplierID = @SupplierId";
using (var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
{
    using (var cmd = new SqlCommand(sql, con))
    {
        con.Open();
        cmd.Parameters.Add("@SupplierId", SqlDbType.Int);
        cmd.Parameters["@SupplierId"].Value = int.Parse(SupplierListBox.SelectedItem.Value);
        using (var rdr = cmd.ExecuteReader())
        {
            if (rdr.Read())
            {
                TextBox1.Text = rdr.GetString(0);
            }
        }
    }
}
于 2013-01-26T22:58:06.657 回答
0

您需要考虑使用参数化查询。试试这个:

    string SupplierListvalue = SupplierListBox.SelectedItem.Value; //SupplierListvalue retrieves the SupplierID value
    ...
    SqlCommand cmd = new SqlCommand("select Country from SupplierDB WHERE SupplierID = @supplierId", conn);
    cmd.Parameters.AddWithValue("@supplierId", SupplierListvalue);
    ...

祝你好运。

于 2013-01-26T22:59:47.527 回答
0

我以错误的方式接近它。基本上以下是正确的代码:

<asp:SqlDataSource ID="SupplierCompanyDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROGConnectionString %>"
SelectCommand="SELECT [Company], [SupplierID] FROM [SupplierDB] WHERE ([SupplierID] &gt;= @SupplierID)"> <SelectParameters> <asp:Parameter DefaultValue="14" Name="SupplierID" Type="Int32" /></SelectParameters> </asp:SqlDataSource>

<asp:SqlDataSource ID="SupplierCountryDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROGConnectionString %>" SelectCommand="SELECT [SupplierID], [Country] FROM [SupplierDB] WHERE ([SupplierID] = @SupplierID)"> <SelectParameters> <asp:ControlParameter ControlID="SupplierListBox" Name="SupplierID" PropertyName="SelectedValue" Type="Int32" /></SelectParameters></asp:SqlDataSource>

这完成了工作!所以现在当我点击 listbox1 时, listbox2 也会被点击,类似于级联。

于 2013-01-27T21:41:24.943 回答