简体   繁体   English

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

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

I am currently developing a C# Windows Form Application. 我目前正在开发一个C#Windows窗体应用程序。

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. 现在我尝试使用SQL命令从数据库中检索信息,以填写我在应用程序中需要的信息。

A sample query would be "select * from Member" 示例查询将是“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 我已经创建了一个成员类,其中包含所有这些值的所有set和get方法

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 . 您可以使用ExecuteScalarExecuteReader加载数据,而无需填充中间DataSet 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 : 然后,在if使用ExecuteReader ,并直接从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
            });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数据库C#Windows Form应用程序中检索数据? - How to Retrieve data from a database c# windows form application? C#如何在Windows窗体应用程序中的字符串内部进行基本数学运算 - C# How do I do some basic math inside of a string in my Windows Form Application 如何使用Windows Forms C#应用程序在第二个线程中检索信息 - How to retrieve information in a second thread with a Windows Forms C# Application 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 如何将 VBA 宏转换为 C# 以在 Windows 窗体应用程序中使用? - How do I convert my VBA macro to C# to use in windows form application? C# 如何为 Windows 窗体应用程序创建安装? - C# How do I create an installation for a windows form application? 如何在C#WIndows表单中附加视频? - How do I attached a video in my C# WIndows Form? 如何从Windows Form应用程序将ac#变量传递给python脚本 - How do I pass a c# variable to a python script from windows form application 如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数? - How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET? 如何使用SqlDataReader从数据库中检索信息,C# - How to use SqlDataReader to retrieve information from database, c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM