将maskedTextBox与数据库的一个字段进行比较以删除记录 [英] compare maskedTextBox with one field of database for delete record

查看:82
本文介绍了将maskedTextBox与数据库的一个字段进行比较以删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码删除数据库中的记录:
SqlCommand com =新的SqlCommand();
com.CommandText =从Tblletter删除,其中Fldcode =''""+ maskedTextBox4.Text.Trim()+"'';
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();

当然,当我使用文本框而不是maskedtextbox时,上面的代码有效,但是现在它不起作用了.
在我的数据库中,我的字段定义列名称= Fldcode,数据类型= char(15)
并删除我在SQL2008中的代码下使用的记录:
从Tblletter删除,其中Fldcode =''313/131/120/120''

感谢您的关注.

i use under code for delete a record on my database:
SqlCommand com = new SqlCommand();
com.CommandText = "delete from Tblletter where Fldcode =''"+maskedTextBox4.Text.Trim() + "''";
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();

of course above code worked when i used textbox instead maskedtextbox but now it dosn''t work.
in my database,my field define column name=Fldcode, data type=char(15)
and for delete a record i used under code in SQL2008:
delete from Tblletter where Fldcode=''313/131/120/120''

thanks fro your attention.

推荐答案

您需要通过以下代码进行几项改进:
1)处理您的SqlCommand(和Connection(按喜好选择))
2)不要连接字符串.请改用参数化查询.否则,您可能会遭受意外或故意的损坏或删除数据库.
3)不要使用MaskedTextBox进行日期获取:请改用DateTimePicker.它不会允许不良约会,并且也使用户更容易使用.
4)您的意思是在示例中引用两个不同的字段吗?一个使用Fdldate,另一个使用Fldcode
There are a couple of things you need to improve with that code:
1) Dispose of your SqlCommand (and Connection, by preference)
2) Don''t concatenate strings. Use Parametrized queries instead. Otherwise you leave yourself open to accidental or deliberate damage or deletion of your database.
3) Don''t use a MaskedTextBox for date acquisition: use a DateTimePicker instead. It won''t allow bad dates, and is a lot easier for the user to use as well.
4) Did you mean to refer to two different fields in your examples? One uses Fdldate, the other Fldcode


这篇关于将maskedTextBox与数据库的一个字段进行比较以删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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