簡體   English   中英

在C#中出現SQL語法錯誤

[英]Having SQL Syntax error in c#

我正在編寫一個腳本以在錯誤跟蹤系統中添加錯誤報告。 單擊提交按鈕后,將彈出SQL語法錯誤對話框。

這是我的編碼

public partial class AddBugForm : Form
{
    public AddBugForm()
    {
        InitializeComponent();
       Fillcombo();
       Fillcombo1();
       Fillcombo2();
    }

    void Fillcombo()
    {
        string constring = "datasource = localhost; username = root; password = ";
        string Query = "select * from bug.type";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
       MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string type = myReader.GetString("Type_of_bug");
                comboBox1.Items.Add(type);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

     }

      void Fillcombo1()
       {
           string constring1 = "datasource = localhost; username = root; password = ";
           string Query1 = "select * from bug.severity";
           MySqlConnection conDataBase1 = new MySqlConnection(constring1);
           MySqlCommand cmdDataBase1 = new MySqlCommand(Query1, conDataBase1);
           MySqlDataReader myReader;
           try
           {
               conDataBase1.Open();
               myReader = cmdDataBase1.ExecuteReader();

               while (myReader.Read())
               {

                   string severity = myReader.GetString("severity");
                   severity_combo.Items.Add(severity);

               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }

    void Fillcombo2()
    {
        string constring2 = "datasource = localhost; username = root; password = ";
        string Query2 = "select * from bug.priority";
        MySqlConnection conDataBase2 = new MySqlConnection(constring2);
        MySqlCommand cmdDataBase2 = new MySqlCommand(Query2, conDataBase2);
        MySqlDataReader myReader;
        try
        {
            conDataBase2.Open();
            myReader = cmdDataBase2.ExecuteReader();

            while (myReader.Read())
            {

                string priority = myReader.GetString("priority");
                priority_combo.Items.Add(priority);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void submit_button_Click(object sender, EventArgs e)
    {
        string constring = "datasource=localhost;username=root;password=";
        string Query = "INSERT INTO 'bug.bug' (Bug_ID, title, Type_of_bug, software, software_version, description, step_to_reproduction, severity, priority, symptom) values('" + this.bugid_txt.Text+"', '" + this.title_txt.Text + "','" + this.comboBox1.Text + "','" + this.software_txt.Text + "','" + this.software_version_txt.Text + "','" + this.description_txt.Text + "','" + this.step_to_reproduction_txt.Text + "','" + this.severity_combo.Text + "','" + this.priority_combo.Text + "','" + this.symptom_txt.Text + "');";

        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("Saved");
            while(myReader.Read())
            {

            }
        }catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

請幫我 :((((

我在INSERT查詢中看到語法錯誤上下文的兩個問題

首先, INSERT INTO 'bug.bug' ; 刪除那些單引號,否則它是一個文字值,而不是表名。 應該是INSERT INTO bug.bug

第二,從查詢語句的最后刪除分號

.... + this.symptom_txt.Text + "');";
                                  ^.... this semicolon

替換此INSERT INTO 'bug.bug'

INSERT INTO `bug.bug`

您的表名稱被字符串化,並且mysql引擎看不到該表。

您遇到的語法錯誤是什么?

關於插入語句的幾點。

  • 您不應該通過組合值字符串來構建SQL命令字符串,這會導致SQL注入問題並容易引起語法錯誤。 相反,您應該使用參數。 參數還使語法更加簡單。

  • 您應該使用ExecuteNonQuery命令而不是Reader,因為Insert語句不會讀取任何數據

更新的語句(僅使用兩個值使其變小):

string Query = "INSERT INTO bug.bug (Bug_ID, title) values (@id, @title)"

    MySqlConnection conDataBase = new MySqlConnection (constring);
    MySqlCommand cmdDataBase = new MySqlCommand (Query, conDataBase);
    cmdDataBase.Parameters.AddWithValue ("@id", bugid_txt.Text)
    cmdDataBase.Parameters.AddWithValue ("@title", title_txt.Text)
    conDataBase.Open();
    cmdDataBase.ExecuteNonQuerty ();
    MessageBox.Show("Saved");

使用參數可能會解決您的語法錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM