如何从我的C#Windows窗体应用程序的数据库检索信息

如何从我的C#Windows窗体应用程序的数据库检索信息

问题描述:

我目前正在开发一个C#Windows窗体应用程序。

I am currently developing a C# Windows Form Application.

现在我试图使用SQL命令从数据库中检索信息,以填充信息需要在我的应用程序中。

Now I am trying to use a SQL Command to retrieve information from the database to fill in the information that I need to have in my Application.

示例查询将是select * from Member

A sample query would be "select * from Member"

成员表会有变量,如名称,位置等等。

In the member table there would be variables like name, location, etc etc.

如何在我的应用程序中编写代码,使我可以填充我的变量的信息从数据库?

How do I code it in my application such that i can fill up my variables with the information from the database?

我的代码为

private Panel createNotificationPanel(String name, String location, String imageExtension, String alertType, String memberid)
    {

    }

我已经创建了一个成员类,其中包括所有这些值的所有set和get方法

I have already created a member class which includes all the set and get method for all this values

到目前为止:

String connectionString =         ConfigurationManager.ConnectionStrings["connection2"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("select * from alert);
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet dataset = new DataSet();

        conn.Open();
        da.Fill(dataset, "authenticate");
        conn.Close();


        int respond = (int)dataset.Tables["authenticate"].Rows[0]["respond"];

if (respond == 1)
        {
            //to fill in here
        }

检索信息后,我将其添加到列表中

after retrieving the information I am going to add it to a list as follow

List.Add(new MemberAlert("name", "location", "type", "memberID", "imageExtension"));

所以我想知道如何替换数据库中的信息
我不知道如何从这里进行,任何人都可以帮我这个?

so i am wondering how do i replace the information inside with the one in the database I am not sure of how do I proceed from here. can anyone help me with this?

DataSet dataset = new DataSet();
using (SqlConnection connection = 
    new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(
        "select * from alert", connection);
    adapter.Fill(dataset, "authenticate");
}

// Load Data from the DataSet into the ListView
private void LoadList()
{
    // Get the table from the data set
    DataTable dtable = dataset.Tables["authenticate"];

    // Clear the ListView control
    listView1.Items.Clear();

    // Display items in the ListView control
    for (int i = 0; i < dtable.Rows.Count; i++)
    {

        DataRow drow = dtable.Rows[i];

        // Define the list items
        ListViewItem lvi = new ListViewItem(drow["name"].ToString());
        lvi.SubItems.Add (drow["location"].ToString());
        lvi.SubItems.Add (drow["type"].ToString());
        lvi.SubItems.Add (drow["memberID"].ToString());
        lvi.SubItems.Add (drow["imageExtension"].ToString());

        // Add the list items to the ListView
        listView1.Items.Add(lvi);
    }
}



如果您需要从数据集表中创建列表,您可以如上所述迭代行,并在循环中创建列表项,并将它们添加到列表中。

if you need to create list out of dataset table. you can iterate rows as above and create list items inside the loop and add them to list.