0

私は次のことをしようとしています:

私は関数を持っています..SQLをクエリし、データベースからいくつかのデータを取得します..今、フォーム上のリストビューにそれらの値を表示したいと思います。関数は SQL クラスにあります。

これは 1 つのアイテムに対してのみ機能しますが、たとえば 5 つのアイテムを渡す場合は、最後にしか渡されないため機能しません。これは、コードを見ると論理的です。

listvieオブジェクトをフォームから関数に「参照」してアイテムを渡す方法がわかりません。

ここにコードがあります:(私はそれが今1つのアイテムしか返さないことを知っています、私は別のことを試しました..どれもうまくいきませんでした):

    public ListViewItem SQL_GetLogsHistory(string WKS)
    {
        SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
        mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
        SqlDataReader reader = mySQLCommand.ExecuteReader();
        ListViewItem item = new ListViewItem();


        while (reader.Read())
        {
            item = new ListViewItem(new[] { 

                reader.GetValue(4).ToString(), // Timestamp
                reader.GetValue(2).ToString() // Log

            });

        }

        reader.Close();
        return item;
    }
4

3 に答える 3

3

あなたはアイテムのリストを持つことができます

List<ListViewItem> items = new List<ListViewItem>();

ループ内で新規作成しListViewItem、プロパティを設定してリストに追加します

    while (reader.Read())
    {
        ListViewItem item = new ListViewItem();
        item = new ListViewItem(new[] { 

            reader.GetValue(4).ToString(), // Timestamp
            reader.GetValue(2).ToString() // Log

        });
      items.Add(item);

    }

最後itemsにメソッドから戻り、以下のようにメソッドのシグネチャを変更します

public List<ListViewItem> SQL_GetLogsHistory(string WKS)

補足: andusingステートメントを使用することをお勧めしますSqlCommandSqlDataReader

public List<ListViewItem> SQL_GetLogsHistory(string WKS)
{
    List<ListViewItem> items = new List<ListViewItem>();
    using (SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn))
    {
        mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
        using (SqlDataReader reader = mySQLCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                ListViewItem item =  new ListViewItem(new[] { 
                            reader.GetValue(4).ToString(), // Timestamp
                            reader.GetValue(2).ToString() // Log
                        });
                items.Add(item);
            }
        }

    }
    return items;
}
于 2013-06-14T08:03:24.277 に答える
3

利回りリターンを使用できます:

   public IEnumerable<ListViewItem> SQL_GetLogsHistory(string WKS)
    {
        SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
        mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
        SqlDataReader reader = mySQLCommand.ExecuteReader();
        ListViewItem item = new ListViewItem();


        while (reader.Read())
        {
            yield return new ListViewItem(new[] { 

                reader.GetValue(4).ToString(), // Timestamp
                reader.GetValue(2).ToString() // Log

            });

        }

        reader.Close();
        return item;
    }

これにより、データの複数の列挙が削減されます。List<ListViewItem>必要に応じて、このメソッドを使用して を作成できます。

var list = SQL_GetLogsHistory(WKS).ToList();

または、これをリスト ビュー データ ソースとして直接使用することもできます。

listView.DataSource = SQL_GetLogsHistory(WKS);
于 2013-06-14T08:03:41.623 に答える
1

このように返す必要がありますList<ListViewItem>か?

public List<ListViewItem> SQL_GetLogsHistory(string WKS)
{
    SqlCommand mySQLCommand = new SqlCommand(Properties.Settings.Default.SQL_GetLogsHistroy, SQLconn);
    mySQLCommand.Parameters.AddWithValue("@WKS", WKS);
    SqlDataReader reader = mySQLCommand.ExecuteReader();
    List<ListViewItem> items = new List<ListViewItem>();

    while (reader.Read())
    {
        var item = new ListViewItem(new[] { 

            reader.GetValue(4).ToString(), // Timestamp
            reader.GetValue(2).ToString() // Log

        });

        items.Add(item);
    }

    reader.Close();
    return items;
}
于 2013-06-14T08:03:59.303 に答える