连接数据库时简单引号的错误

发布于 2024-11-09 09:06:11 字数 1316 浏览 5 评论 0原文

我有以下代码来添加新用户:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-11-16 09:06:11

您的代码存在多个问题:

  • 代码示例中的第二行是 return true;,这意味着它不会运行
  • InsertUsers 中的任何方法参数没有指定类型
  • 不保持连接打开,检索数据后处置连接,
  • 使用 using 来保证连接关闭/处置,即使发生异常也
  • 使用参数化查询。原因如下:SQL 注入
  • 不要捕获所有异常,仅在这种情况下使用 SqlException

尝试从头开始:

public static bool InsertUser(string userName, string password, string type)
{
    try
    {
        using (var connection = new SqlConnection("data source=.; integrated security=true; initial catalog=test;"))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "insert into users (username, pass, type) values (@username, @password, @type)";
            command.Parameters.AddWithValue("username", userName);
            command.Parameters.AddWithValue("password", password);
            command.Parameters.AddWithValue("type", type);
            connection.Open();
            command.ExecuteNonQuery();
        }
        return true;
    }
    catch (SqlException)
    {
        return false;
    }
}

There is more than one issue with your code:

  • the second line in your code sample is return true;, which means it will not run anything
  • method parameters in InsertUsers do not have type specified
  • don't keep connection open, dispose connection after data are retrieved
  • use using to guarantee connection closing/disposal even if exception happened
  • use parametrized query. Find here why: SQL injection.
  • don't catch all exceptions, SqlException only in this case

Tried to make it from scratch:

public static bool InsertUser(string userName, string password, string type)
{
    try
    {
        using (var connection = new SqlConnection("data source=.; integrated security=true; initial catalog=test;"))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "insert into users (username, pass, type) values (@username, @password, @type)";
            command.Parameters.AddWithValue("username", userName);
            command.Parameters.AddWithValue("password", password);
            command.Parameters.AddWithValue("type", type);
            connection.Open();
            command.ExecuteNonQuery();
        }
        return true;
    }
    catch (SqlException)
    {
        return false;
    }
}
你的背包 2024-11-16 09:06:11

您重新发现了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文