[英]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
. 您可以使用
ExecuteScalar
和ExecuteReader
加载数据,而无需填充中间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.