我想将form1列表框的选定项目的数据库中的数据传递给form2文本框。 [英] I want to pass a data from database of a selected item of listbox of form1 to form2 textbox.

查看:106
本文介绍了我想将form1列表框的选定项目的数据库中的数据传递给form2文本框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我在列表框中有一个项目列表,当我选择一个时,应该在数据库的form2文本框中显示它的价格。



我的尝试:



////////////////////// //// form1

string SqlStatement5 =从Menu1中选择价格,其中Item_Deatail ='+ listBox1.Text +';



DataSet dse6 = Menuclass.GetData(SqlStatement5);

Menuclass._set = dse6.ToString();



////// ////////////////////////////// form2

textBox2.Text = Menuclass._set;

It's like I have a list of items in a listbox and when i select one the price of that should be dispalyed in a textbox of form2 from database .

What I have tried:

//////////////////////////form1
string SqlStatement5 = "select Price from Menu1 where Item_Deatail='"+listBox1.Text+"'";

DataSet dse6 = Menuclass.GetData(SqlStatement5);
Menuclass._set = dse6.ToString();

//////////////////////////////////// form2
textBox2.Text = Menuclass._set;

推荐答案

首先,不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



然后你需要传递信息。麻烦的是你究竟是怎么做的,取决于两种形式之间的关系。

看看这些,其中一种适合你的情况。

创建另一个实例的表单:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then you need to pass the information. Trouble is that exactly how you do that depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:

MyForm mf = new MyForm();
mf.Show();

是父,另一种形式是孩子。

(这并不意味着任何正式的MDI关系)



转让两种表格之间的信息,第1部分:父母与子女 [ ^ ]

在两种表格之间传递信息,第2部分:儿童到父母 [ ^ ]

在两种形式之间传递信息,第3部分:儿童到儿童 [ ^ ]

Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]


这篇关于我想将form1列表框的选定项目的数据库中的数据传递给form2文本框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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