【问题标题】:Unclosed quotation mark after the character string字符串后的非闭合引号
【发布时间】:2021-07-12 18:51:28
【问题描述】:

我正在开发一个密码存储应用程序,用户必须输入文件名,从组合框中选择文件扩展名,然后在提供的文本框中键入文件内容。文件名和扩展名仅适用于他们选择导出它。我插入数据库的大部分数据工作正常,并且可以毫无问题地插入/能够从 SQL Server 数据库中检索。但是一旦我的文件内容包含诸如“”/'/()/等字符,它就会给我错误

System.Data.SqlClient.SqlException: 't' 附近的语法不正确。 字符串')'后的非闭合引号。'

如何才能将文件内容字符串插入到数据库中,与输入到文本框中的内容完全相同:这是单击保存文件按钮后插入的代码,其中出现初始错误:

private void btnSaveFile_Click(object sender, EventArgs e)
{
    string extensionType = comboExtension.GetItemText(comboExtension.SelectedItem);

    if (txtFileName.Text == "")
    {
        MessageBox.Show("Please Enter A Valid File Name", "Please Fill In", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    else if (!txtFileName.Text.All(char.IsLetterOrDigit))
    {
        MessageBox.Show("File Name can only contain letters or numbers, No Special Characters", "Incorrect Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtFileName.Clear();
    }
    else if (extensionType == "")
    {
        MessageBox.Show("Please Select An Extension Type", "Select File Type", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else if (txtDescription.Text.Length > 7999)
    {
        MessageBox.Show("File content cannot exceed 8000 characters", "Characters Exceeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else {

        string fname = txtFileName.Text;
        string content = txtDescription.Text;
        string forUser = Form1.userId;
        string dateAdded = DateTime.Now.ToString();


        connection.Open();
        cmd = new SqlCommand("insert into [Files] values('" + fname + "', '" + extensionType + "', '" + content + "', '" + dateAdded + "', '" + forUser + "')", connection);
        cmd.ExecuteNonQuery();
        connection.Close();

        this.Alert("File Created Successfully");
        this.Alert("File Created Successfully");

        txtFileName.Clear();
        txtDescription.Clear();
        comboExtension.Text = ("File Extension");
    }
}

【问题讨论】:

  • Parameters 会解决你的问题。
  • 神送保护系统强制你使用参数。
  • 你没有使用parameters,你也不是sanitizing你的输入。尝试输入以下内容作为文件名 "myfile';delete files--"
  • @ZwakeleSibisi:你有没有看一下人们发布的链接?有例子。

标签: c# sql-server visual-studio winforms .net-framework-version


【解决方案1】:

永远不要将输入参数转换为 SqlCommand 的一部分!

有恶意的人将能够删除您的完整数据库:

var cmd = new SqlCommand("insert into [Files] values('"
    + fname
    + "', '"
    + extensionType ...

(你有更多参数,但我想你会明白要点)

如果fnameextensionType 具有以下值会发生什么:

string fname = "TextFile.Txt', ExtensionType = `.Txt`, Content = `ABC`);"
   + "DROP DATABASE databasename;";

string extensionType = "insert into [Files] values('HaHaDeletedYourDB.Txt ...

这将使您的 sqlCommand 完整:

Insert info [Files] values('TextFile.Txt', ExtensionType = `.Txt`, Content = `ABC`);
DROP DATABASE databasename;
insert into [Files] values(`HaHaDeletedYourDb.Txt`  ...

您的完整数据库将被删除 因此:应使用DbParameterCollection.AddWithValue添加参数

public void AddToDb(string fileName, string extension, string content)
{
    const sqlText = "insert into [Files] values(@FileName, @Extension, @Content);"
    using (DbConnection dbConnection = new DbConnection(...))
    {
        using (var dbCommand = dbConnection.CreateCommand())
        {
            dbCommand.CommandText = sqlText;
            dbCommand.Parameters.AddWithValue(@FileName, fileName);
            dbCommand.Parameter.AddWithValue(@Extension, extension);
            dbCommand.Parameter.AddWithValue(@Content, content);

            dbConnection.Open();
            dbCommand.ExecuteNonQuery);
        }
    }
}

using 语句将确保 Dispose 始终被调用,即使在异常之后也是如此。 Dispose 将关闭 DbConnection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多