从sql数据库获取数据并添加到列表视图列?

时间:2012-08-14 08:54:16

标签: c# database

我是C#程序员我试图将sql数据库数据添加到我的列表视图但是它出现错误(无法在同一个地方添加或插入)

ListViewItem i = new ListViewItem();
Invoke(new Action(() => listBox1.Items.Clear()));
con = new SqlConnection("Data Source=LEO-PC\\SQLEXPRESS;Initial Catalog = Datashare;Integrated Security = True");
cmd = new SqlCommand("select * from shareTable", con);
con.Open();
SqlDataReader sqlRead;
try
{
    sqlRead = cmd.ExecuteReader();
    while (sqlRead.Read())
    {

        string fileNameStr = (string)sqlRead["filePath"];
        string author = (string)sqlRead["authorName"];
        DateTime uploadTime = (DateTime)sqlRead["dateSend"];
        string date = Convert.ToString(uploadTime); ;


        Invoke(new Action(() => i.SubItems.Add(author)));
        Invoke(new Action(() => i.SubItems.Add(fileNameStr)));
        Invoke(new Action(() => i.SubItems.Add(date)));
        listView1.Items.Add(i);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    con.Close();
}

0 个答案:

没有答案