2017-02-26 103 views
0

我想從數據庫中提取一些數據並填充acombobox並獲取以下錯誤: 拋出異常:'MySql.Data.MySqlClient.MySqlException'in MySql.Data.dll 附加信息:您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行使用near'from clienti order by nume'使用正確的語法MySql.Data.MySqlClient.MySqlException'在MySql.Data.dll中

有趣的是,只有在插入「auto 「組合框,它工作過。

我的代碼:

public partial class Inchirieri : Form 
    { 

     static string connstr = "Data source=localhost;UserId=root;database=inchirieri_auto"; 
     static MySqlConnection conn = new MySqlConnection(connstr); 

     static MySqlCommand comAuto = new MySqlCommand("Select cod_auto, Concat(marca,' ',model,' ',cast(an as char(4))) as date_auto from autoturisme order by marca", conn); 
     MySqlDataAdapter adaptA = new MySqlDataAdapter(comAuto); 
     DataTable autoT = new DataTable(); 

     static MySqlCommand comClient = new MySqlCommand("Select ID_Client, Concat(nume,' ',prenume as numecli from clienti order by nume", conn); 
     MySqlDataAdapter adaptc = new MySqlDataAdapter(comClient); 
     DataTable clientT = new DataTable(); 



     public Inchirieri() 
     { 
      conn.Open(); 
      InitializeComponent(); 
      Completez_Combo_Client(); 
      Completez_Combo_Auto(); 
     } 
     void Completez_Combo_Auto() 
     { 
      adaptA.Fill(autoT); 
      auto.Items.Clear(); 
      auto.DataSource = autoT; 
      auto.ValueMember = "cod_auto"; 
      auto.DisplayMember = "date_auto"; 

     } 
     void Completez_Combo_Client() 
     { 
      adaptc.Fill(clientT); 
      client.Items.Clear(); 
      client.DataSource = clientT; 
      client.ValueMember = "ID_Client"; 
      client.DisplayMember = "numecli"; 

     } 
+0

使用'try'和'catch'。你會得到確切的異常並且很容易解決它。我認爲在查詢語法中存在一些問題。 – Venky

回答

1

你是做了一個錯誤。在查詢中關閉Concat的括號。這應該工作

static MySqlCommand comClient = new MySqlCommand("Select ID_Client, Concat(nume,' ',prenume) as numecli from clienti order by nume", conn); 
     MySqlDataAdapter adaptc = new MySqlDataAdapter(comClient); 
+0

謝謝穆罕默德!這是問題。 – Mircea

相關問題