从列表框向数据库添加数据 [英] add data to a database from a listbox

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

问题描述

我正在制作Windows窗体,该窗体允许人们从不同类型的电影中选择电影.我使用列表框来显示电影和流派的列表.当用户选择一种流派时,该流派的电影将显示在另一个列表框中.用户从该列表框中选择影片,然后将影片中的图片显示在图片框中,并将有关该影片的信息显示在文本框中.我还添加了一个按钮,用户单击该按钮后,一旦他们选择了电影,就会显示一个带有按钮的消息框,询问他们是否确定要选择这部电影.如果用户单击是",则电影的图片和信息仍将像以前一样显示在屏幕上.如果用户单击否",则会从屏幕上清除图片和信息,并且用户可以选择另一部电影.

现在,我要做的就是将选择的影片存储在数据库中.因此,我可以记录用户选择的电影.问题是我不知道如何基于列表框中的选择将数据发送到数据库中.因此,例如,如果我选择盗梦空间"并使用带有按钮的消息框确认选择,是否有办法通过单击是"按钮将该电影选择存储到数据库中,还是我需要做其他事情? div class ="h2_lin">解决方案

您需要处理单击按钮的事件.

处理完之后,您可以通过listbox.SelectedItem在列表框中获取当前选定的项目.

然后,您所需要做的就是通过ADO.net将相关值传递到数据库中的存储过程中.


您是否表示您正在使用带有YesNo按钮的MessageBox? br/>
如果是这样,则需要捕获MessageBox的结果并对照DialogResult进行检查.是,如下所示:

 DialogResult结果;
结果= MessageBox.Show("  保存初始状态",MessageBoxButtons.YesNo);
如果(结果== DialogResult.是)
  // 插入数据库 


I''m making a Windows form which allows people to select a film from different genres. I''ve used list boxes to display the list of films and genres. When a user selects a genre, the films from that genre are displayed in another list box. The user selects a film from that list box, then a picture from the film is displayed in a picture box and informtion for the film is displayed in a text box. I have also added a button which the user clicks, once they have selected the film, a message box is displayed with buttons, that asks if they''re sure they want to choose this film. If the user clicks "Yes", the picture and info for the film are still displayed on screen as before. If user clicks "No" then the picture and info are cleared from the screen and the user can select another film.

Now I what I want to do is store that film choice in a database. So I can keep a record of the films chosen by the user. The problem is I dont know how to send data into a database based on a selection from a listbox. So for example if I selected "Inception" and confirmed my selection using the message box with buttons, is there a way to store that film choice into the database just by clicking the "Yes" button or would I need to do something different?

解决方案

You need to handle the event of the click button.

Once that is handled you can get the current selected item in the listbox by listbox.SelectedItem .

All you then need to do is pass in the relevant values to a Stored Procedure in the database via ADO.net


Do you mean that you''re using a MessageBox with YesNo buttons?

If so, you need to capture the result of the MessageBox and check it against DialogResult.Yes as in:

DialogResult result;
result = MessageBox.Show("Do you want to save this?","Save Inception",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
  //insert into database


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

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