如何将dynMically创建的文本框中的值插入到db中 [英] how to insert values from dynMically created textbox into db

查看:109
本文介绍了如何将dynMically创建的文本框中的值插入到db中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建了文本框。我想从该文本框上传数据到db bt我不会工作..



i am created textbox dynamically . i want to upload datas from that textbox to db bt i wont works..

TextBox t = new TextBox();
          t.Location = new System.Drawing.Point(379, 60);
          this.Controls["dynamicTextBox"].Text = "my text";
          t.Text = "What are you doing now";
          t.Multiline = true;
          t.Size = new System.Drawing.Size(430, 55);
          panel2.Controls.Add(t);





还有一件我无法做到的事情t.id = DD; id显示错误。所以我尝试t.name =dd但插入db sqlcommand我无法插入值insert into tablename values'+ dd.text +',c。

dd.text显示错误。

请纠正或提供有效代码



and one more thing i cannot able to ad t.id="dd"; id shows error. so i try t.name="dd" but insert db sqlcommand i cannot insert values "insert into tablename values '"+ dd.text+"'",c.
dd.text shows error.
kindly rectify or provide valid code

推荐答案

文本框是否动态创建无关紧要。基本原则总是一样的:

- 从控件获取值

- 将值设置为参数

- 执行参数化语句。



换句话说,你应该使用一个使用参数的SQL语句,如 SqlParameter [ ^ ]
It doesn't matter if the text box is created dynamically or not. The basic principle is always the same:
- get the value from a control
- set the value into a parameter
- execute the parameterized statement.

In other words, you should use an SQL statement which uses parameters as explained in SqlParameter[^]


除了solution1通过 Mika Wendelius [ ^ ],强烈建议您阅读本文:创建动态数据输入用户界面 [ ^ ]



有关详细信息,请参阅:

使用验证创建动态文本框和下拉列表 [ ^ ]

将多个动态文本框值保存到SQL数据库Windows Forms C# [ ^ ]
In addition to solution1 by Mika Wendelius[^], it's strongly recommended to read this article: Creating Dynamic Data Entry User Interfaces[^]

For further information, please see:
Create dynamic Textbox and Dropdownlist with Validation[^]
Save multiple Dynamic Textbox values to SQL database Windows Forms C#[^]


这篇关于如何将dynMically创建的文本框中的值插入到db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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