将数据保存到数据库,然后将数据加载到列表框 [英] Save data to database then load data to Listbox

查看:96
本文介绍了将数据保存到数据库,然后将数据加载到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个按钮添加到 CheckListBox



第二个按钮添加了一个代码,其中包含交货名称和交货地址



我还有一个名为customers的数据库表,其中包含以下列:
ID,说明,客户名称,客户地址,到达时间,交货名称,交货地址



我现在有大约10条记录存储在数据库中



我的问题 - 我如何编码它,以便当我的程序启动它将存储在我的数据库中的记录加载到 CheckListBox 中,当我添加新的交货或新的接收时,它将它保存在我的数据库的Customer表中?此外,如果我在 CheckListBox 中编辑或删除,我想要更新我的数据库表。

解决方案

从视频的外观,你使用SQL Server。你需要做一些事情,以便得到你想要的程序。我会尽力让你在那里,与提供的信息(这假设你正在学习,将保持基本的):



如何编码当我的程序启动它加载存储在我的数据库中的记录到CheckListBox



你需要添加这个使用语句在窗体窗体类的顶部:

 使用System.Data.SqlClient;然后,在form_Load事件中,连接到您的数据库,并从客户表中检索行(不是$表示)测试):

  private void Form1_Load(object sender,EventArgs e)
{
//设置连接到您的数据库。
SqlConnection myConnection = new SqlConnection(user id = sql_userID;+
password = password; server = server_url;+
Trusted_Connection = yes;+
= databaseName;+
connection timeout = 30);

//打开连接。
myConnection.Open();

//创建数据集以存储信息。
DataSet ds = new DataSet();

//创建命令对象和适配器以检索信息。
SqlCommand myCommand = new SqlCommand(SELECT * FROM Customers,myConnection);
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
adapter.Fill(ds);

//循环遍历每一行,并显示要在CheckListBox中显示的任何列。
foreach(ds.Tables中的DataRow行)
checkedListBox1.Items.Add(row [ColumnNameToShow]);
}

你的问题的其余部分有点含糊,因为你不解释您将要保存一个新记录(具有按钮,需要什么数据,用户实际输入什么数据,输入类型等)或如何删除记录。这应该让你在正确的路径,并帮助让你开始。


I have 2 buttons that add to a CheckListBox. The first button adds a Delivery with the customer name, address and arrival Time.

The second button adds a pickup with Delivery Name and Delivery address

I also have a database table called customers with the following columns: ID, Description, Customer Name, Customer Address, Arrival Time, Delivery Name, Deliver Address

I have about 10 records stored in the database at the moment

My question - how do I code it so that when my program starts it loads the records stored in my database into the CheckListBox and when I add a new delivery or a new pick up it saves it in the Customer table in my database? Also, if I edit or delete within the CheckListBox I want it to update my database table accordingly.

解决方案

From the looks of the video, you're using SQL Server. You'll need to do a few things in order to get your program where you want it. I'll try my best to get you there, with the information provided (this assumes you are learning and will keep things basic):

"How do I code it so that when my program starts it loads the records stored in my database into the CheckListBox"

You'll need to add this using statement at the top of your windows form class:

using System.Data.SqlClient;

Then, in the form_Load event, connect to your database and retrieve the rows from the customer table (not tested):

        private void Form1_Load(object sender, EventArgs e)
    {
        //Setup connection to your database.
        SqlConnection myConnection = new SqlConnection("user id=sql_userID;" +
                                   "password=password;server=server_url;" +
                                   "Trusted_Connection=yes;" +
                                   "database=databaseName; " +
                                   "connection timeout=30");

        //Open connection.
        myConnection.Open();

        //Create dataset to store information.
        DataSet ds = new DataSet();

        //Create command object and adapter to retrieve information.
        SqlCommand  myCommand = new SqlCommand("SELECT * FROM Customers", myConnection);
        SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
        adapter.Fill(ds);          

        //Loop through each row and display whichever column you wish to show in the CheckListBox.
        foreach (DataRow row in ds.Tables)
            checkedListBox1.Items.Add(row["ColumnNameToShow"]);
    }

The rest of your question is a bit vague because you don't explain how you are going to save a "new" record (with a button, what data is required, what data is the user actually inputting, input types, etc.) or how you are "deleting" a record. This should get you on the right path though and help get you started.

这篇关于将数据保存到数据库,然后将数据加载到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆