删除表中的记录 [英] delete records in table

查看:64
本文介绍了删除表中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小型vb.net项目.当我删除
按名称记录显示错误消息,例如
这个"

i create small vb.net project. when i delete
record by name display erro message like
this "

system.data.sqlclient.sqlexception:invalid
column name ''jak''. at system .data.........


但实际上"jak"和他的信息已保存
在桌子上.然后我尝试通过
删除他的信息 他的年龄.它的工作.但我不知道
按名称删除信息.我的代码是


but actualy ''jak'' and his infomation saved
in table. then i try delete his infomation by
his age. its worked. but i have no idea
delete infomation by names. my code is

try
Dim del As new sqlcomand("DELETE FROM
members WHERE name="& textbox1.text
& "", dbcon)
dbconn.open()
del.execuenonquery()
dbconn.close()
Catch ex as Exception
Msgbox(ex.tostring)
end try


它是corect吗?
请朋友帮我.我正在使用vb.net
和SQL服务器.表名是成员.有两个领域.其名称(文字)和年龄(int)
谢谢.


is it corect??
please friends help me. I''m using vb.net
and sql server. table name is members. there have two fields. its name(text) and age(int)
thank you.

推荐答案

首先,您不应该那样做-这是对SQL Injection攻击的邀请,它可能会破坏或破坏您的数据库.改用参数化查询:
Firstly, you shouldn''t do it like that - it is an invitation to an SQL Injection attack, which can damage or destroy your database. Use Parametrized queries instead:
Dim del As new SqlCommand("DELETE FROM members WHERE name=@NM", dbcon)
del.Parameters.AddWithValue("@NM", textbox1.Text)



其次,您可能会发现问题也消失了!



Secondly, you may find your problem disappears as well!


代码是正确的,但是在编码和创建数据库表时,请遵循关键字名的命名.
Code is correct but at the time of coding and database table creation abide Naming on the name of Keywords.


试试这个
Dim del As new SqlCommand("DELETE FROM members WHERE name=@NM", dbcon)
del.Parameters.AddWithValue("@NM", textbox1.Text)





or

Dim del As new sqlcomand("DELETE FROM
members WHERE name='"& textbox1.text
& "'", dbcon)


这篇关于删除表中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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