连接数据库时简单引号的错误
我有以下代码来添加新用户:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
public class users
{
public Sqlconnection myconn ()
{
return new ("data source=.; integrated security=true; initial catalog=test;");
}
public bool insertuser(username, pass, type)
{
try {
string query="insert into users (username, pass, type) values ( '"+username+"', '"+pass+"', '"+type+"');
return true;
SqlCommand mycommand = new SqlCommand (query, this.myconn);
mycommand.Connection.Open();
mycommand.ExecuteNonQuery();
mycommand.Connection.Close();
return true;
}
catch {
return false;
}
}
}
如果用户调用此方法,则现在在表单中
users user1 = new users();
if(user1.insertuser(txtusername.tex, txtpass.text, cbtype.text)==true)
{
// BUG IS HERE IF USER WRITE SOMETHING SO.. ANGEL' (WITH THIS ')
// MY CODE IS GOING TO HAVE A BUG!
// I QUIT THEM IN KEY PRESS BUT WHAT HAPPEND IF USERS MUST TO ADD SOMETHING AS
// tic's
// what can i do for my code acept all?? and it doesn't have any bug?
MessageBox.show("user added");
}
I have the following code to add a new user:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
public class users
{
public Sqlconnection myconn ()
{
return new ("data source=.; integrated security=true; initial catalog=test;");
}
public bool insertuser(username, pass, type)
{
try {
string query="insert into users (username, pass, type) values ( '"+username+"', '"+pass+"', '"+type+"');
return true;
SqlCommand mycommand = new SqlCommand (query, this.myconn);
mycommand.Connection.Open();
mycommand.ExecuteNonQuery();
mycommand.Connection.Close();
return true;
}
catch {
return false;
}
}
}
now in the form if user calls to this method
users user1 = new users();
if(user1.insertuser(txtusername.tex, txtpass.text, cbtype.text)==true)
{
// BUG IS HERE IF USER WRITE SOMETHING SO.. ANGEL' (WITH THIS ')
// MY CODE IS GOING TO HAVE A BUG!
// I QUIT THEM IN KEY PRESS BUT WHAT HAPPEND IF USERS MUST TO ADD SOMETHING AS
// tic's
// what can i do for my code acept all?? and it doesn't have any bug?
MessageBox.show("user added");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码存在多个问题:
return true;
,这意味着它不会运行using
来保证连接关闭/处置,即使发生异常也尝试从头开始:
There is more than one issue with your code:
return true;
, which means it will not run anythingusing
to guarantee connection closing/disposal even if exception happenedTried to make it from scratch:
您重新发现了 SQL 注入攻击。
不要在 SQL 中包含外部派生的值。
改用参数化查询。
您显示的代码无论如何都无法编译(Sqlcommand 与 SqlCommand),但请阅读 this MSDN 页面(或仅搜索有关参数化查询或 SQL 注入的信息)以获取更多信息。
You've rediscovered SQL injection attacks.
Don't include externally-derived values in your SQL.
Use parameterized queries instead.
The code you've shown wouldn't compile anyway (Sqlcommand vs SqlCommand) but read this MSDN page (or just search for information on parameterized queries or SQL injection) for more information.