如何使用Objects编写插入查询 [英] How to write insert Query using Objects

查看:74
本文介绍了如何使用Objects编写插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将表单设计作为id,名称,描述,由用户创建,修改...等等

在提交按钮中单击我正在编写用于插入值的代码



按钮单击功能
{
SqlConnection string = new SqlConnection()
string query = 插入emp(id,name,modif,cre,user)值( + id.Text + + Name.Text + + modif.Text + + created.Text + + user.Text + < span class =code-string>)
SqlDataAdapter da = new SqlD ataAdapter(query,con)
DataSet ds = new DataSet()
da.Fill(ds)
}



这里我需要在查询部分中升级代码

1)我需要一次获取所有文本框详细信息,它应该更新到查询而不用写为id .text + name.text .....

i意味着可以一次调用所有文本框详细信息并追加查询。

这个我们怎么写代码以对象的形式获取所有文本框详细信息并附加到查询。



请帮助我编写代码,以对象的形式获取所有文本框详细信息然后追加查询。

解决方案



我认为你不能把所有的TextBox都作为一个单一的宾语。在应用程序中,每个TextBox本身都是Common类TextBox的对象。所以不要让事情太复杂。您提供的代码是使用TextBox的最佳方式。为了避免SQLInjection,您可以为每个文本框定义MaxLength属性。


您可以像使用此插入查询一样使用更新查询。



另外请注意,不要以这种方式编写查询 - 使用命令参数 - http://en.wikipedia.org / wiki / SQL_injection [ ^ ]。

i Have form design as id,Name,description,created by,modified by,user...as so on
in Submit button click i am writing code for inserting values as

Button Click Function
{
SqlConnection string= new SqlConnection()
string query = "insert into emp(id,name,modif,cre,user)values(" + id.Text + "," + Name.Text + "," + modif.Text + "," + created.Text + "," + user.Text + ")"
SqlDataAdapter da = new SqlDataAdapter(query, con)
DataSet ds = new DataSet()
da.Fill(ds)
}


here i need Upgrade code in Query part
1)i need get the all textbox details at a time and it should update to the query without writing as id.text+name.text .....
i mean is it possible to call all textbox details at single time and append to query.
for this how can we write code in taking all textbox details in the form of objects.and appending to the query.

please help me who to write code getting all textbox details in the form of objects and appending to query.

解决方案

Hi,
I don''t think you can get all the TextBox''s as a single object. Here in the application each TextBox is itself an object of Common class TextBox. So don''t make things too complicated. The code which you presented is the best way to use TextBox. To avoid SQLInjection, you may define MaxLength attribute for each Textbox.


You can use Update queries just like you used this insert query.

On another note, don''t write queries in this way - use command parameters - http://en.wikipedia.org/wiki/SQL_injection[^].


这篇关于如何使用Objects编写插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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