c# - SQL 语法错误(INSERT 命令)

标签 c# sql

我有一个带有文本框和按钮的表单,这样当用户单击按钮时,文本框中指定的名称将添加到我的 sql 数据库中的表中。按钮的代码如下:

private void btnAddDiaryItem_Click(object sender, EventArgs e)
{
   try
   {
       string strNewDiaryItem = txtAddDiaryItem.Text;
       if (strNewDiaryItem.Length == 0)
       {
           MessageBox.Show("You have not specified the name of a new Diary Item");
           return;
       }
       string sqlText = "INSERT INTO tblDiaryTypes (DiaryType) VALUES = ('" + strNewDiaryItem + "');";
       cSqlQuery cS = new cSqlQuery(sqlText, "non query");
       PopulateInitialDiaryItems();
       MessageBox.Show("New Diary Item added succesfully");
   }
   catch (Exception ex)
   {
       MessageBox.Show("Unhandled Error: " + ex.Message);
   }
}

类cSqlQuery是一个简单的类,为我执行各种T-SQL操作,它的代码如下:

class cSqlQuery
{
    public string cSqlStat;
    public DataTable cQueryResults;
    public int cScalarResult;

    public cSqlQuery()
    {
        this.cSqlStat = "empty";
    }

    public cSqlQuery(string paramSqlStat, string paramMode)
    {
        this.cSqlStat = paramSqlStat;

        string strConnection = BuildConnectionString();
        SqlConnection linkToDB = new SqlConnection(strConnection);

        if (paramMode == "non query")
        {
            linkToDB.Open();
            SqlCommand sqlCom = new SqlCommand(paramSqlStat, linkToDB);
            sqlCom.ExecuteNonQuery();
            linkToDB.Close();
        }

        if (paramMode == "table")
        {
            using (linkToDB)
            using (var adapter = new SqlDataAdapter(cSqlStat, linkToDB))
            {
                DataTable table = new DataTable();
                adapter.Fill(table);
                this.cQueryResults = table;
            }
        }

        if (paramMode == "scalar")
        {
            linkToDB.Open();
            SqlCommand sqlCom = new SqlCommand(paramSqlStat, linkToDB);
            this.cScalarResult = (Int32)sqlCom.ExecuteScalar();
            linkToDB.Close();
        }
    }

    public cSqlQuery(SqlCommand paramSqlCom, string paramMode)
    {
        string strConnection = BuildConnectionString();
        SqlConnection linkToDB = new SqlConnection(strConnection);
        paramSqlCom.Connection = linkToDB;

        if (paramMode == "table")
        {
            using (linkToDB)
            using (var adapter = new SqlDataAdapter(paramSqlCom))
            {
                DataTable table = new DataTable();
                adapter.Fill(table);
                this.cQueryResults = table;
            }
        }

        if (paramMode == "scalar")
        {
            linkToDB.Open();
            paramSqlCom.Connection = linkToDB;
            this.cScalarResult = (Int32)paramSqlCom.ExecuteScalar();
            linkToDB.Close();
        }
    }

    public string BuildConnectionString()
    {
        cConnectionString cCS = new cConnectionString();
        return cCS.strConnect;
    }        
}

该类在我的整个应用程序中运行良好,因此我认为错误不在该类中,但我不能确定。

当我点击按钮时,我收到以下错误消息:

Incorrect syntax near =

这让我很恼火,因为当我在 SQL Management Studio 中运行完全相同的命令时,它工作正常。

我确定我遗漏了一些相当简单的东西,但是在多次阅读我的代码之后,我很难找出哪里出了问题。

最佳答案

您必须在值之后删除 =。

string sqlText = "INSERT INTO tblDiaryTypes (DiaryType) VALUES ('" + strNewDiaryItem + "');"

并尝试使用参数化查询来避免Sql注入(inject)。像这样使用你的代码。 Sql Parameters

 string sqlText = "INSERT INTO tblDiaryTypes (DiaryType) VALUES (@DairyItem);"
    YourCOmmandObj.Parameters.AddwithValue("@DairyItem",strNewDiaryIItem) 

关于c# - SQL 语法错误(INSERT 命令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9064288/

相关文章:

c# - 定义集合元素的 LINQ 语句问题

C# SqlDataReader for int

sql - 运行 plsql block 时出现无效标识符错误

sql - 使用 "to_tsquery"和 "simple"时,tsvector 上的 "english"会产生不同的结果?

c# - 如果元素在表格上的元素内部有文本,则测试失败

c# - Windows 与 Linux 上的图形库

sql - 最简单的 SQL 查询永远不会返回

php - 如何从一个输入 Html 表单插入数组字符串并从中获取另一数据

c# - Base64反序列化过程中的空引用异常(C#)

php - 我的日期未以日期时间格式正确保存到 MySQL