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

何が主な問題なのかはっきりしないので、CountryADO.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

私は間違った方法でそれに近づいていました。基本的に、次のコードが正しいコードでした。

<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 に答える
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 に答える