简体   繁体   中英

How do I retrieve information from database for my C# Windows Form Application

I am currently developing a C# Windows Form Application.

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.

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?

My code for the method would be

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

    }

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

and currently what I have done so far is :

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.

You can load your data without having to fill an intermediate DataSet by using ExecuteScalar and ExecuteReader . So something like the following:

cmd.CommandText = "select respond from alert";
var respond = cmd.ExecuteScalar();

if (respond != null && (int)respond == 1)
{
   //... execute your command to select from Member here
}

Then, inside your if use ExecuteReader , and load the values directly from the SqlDataReader :

cmd.CommandText = "select * from Member";

using (var reader = cmd.ExecuteReader())
{
    while(reader.Read())
    {
        list.Add(new MemberAlert
            {
                Name = reader.GetString(0),
                // load other properties from reader
            });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM