2015-10-10 57 views
0

每當我跑我的代碼,它將返回此特定錯誤爲什麼我得到:命令執行過程中遇到致命錯誤

個MySqlException是由用戶代碼未處理:命令執行過程中遇到致命錯誤

指向

long count = (long)cmd1Database.ExecuteScalar(); 

這裏有什麼問題?我對c#完全陌生。任何幫助將非常感激。

string constring = "Database=chtbuster;Data Source=localhost;User Id=root;Password='';"; 
string count1 = "SELECT COUNT(hostName) FROM chtbuster.processtable WHERE hostName = @machineName; "; 
using (MySqlConnection conDataBase = new MySqlConnection(constring)) 
{ 
    conDataBase.Open(); 

    string insertprocess = "INSERT INTO chtbuster.processtable(processID,processFileName,processName,processPath,hostName) VALUES (@processID, @fileName, @exeFile, @filePath, @machineName);"; 
    MySqlCommand cmd2Database = new MySqlCommand(insertprocess, conDataBase); 

    cmd2Database.Parameters.AddWithValue("@processID", processID); 
    cmd2Database.Parameters.AddWithValue("@filename", fileName); 
    cmd2Database.Parameters.AddWithValue("@exeFile", exeFile); 
    cmd2Database.Parameters.AddWithValue("@filePath", filePath); 
    cmd2Database.Parameters.AddWithValue("@machineName", machineName); 
    cmd2Database.ExecuteNonQuery(); 

    //string checkname = "SELECT hostName FROM chtbuster.hostnametable WHERE ([email protected]); "; 
    MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase); 
    long count = (long)cmd1Database.ExecuteScalar(); 
    if (count == 1) 
    { 
     listBox1.Items.Add(new MyListBoxItem(Color.Gold, count + "st warning" + machineName + " has opened " + fileName + " from path " + filePath)); 
     if(machineName == pc1.Text) 
     { 
      pc1.Image = Image.FromFile("../../firstwarning.png"); 
     } 
     else if (machineName == pc2.Text) 
     { 
      pc2.Image = Image.FromFile("../../firstwarning.png"); 
     } 
     else 
     { 
      pc3.Image = Image.FromFile("../../firstwarning.png"); 
     } 

    } 
    else if (count == 2) 
    { 
     listBox1.Items.Add(new MyListBoxItem(Color.Orange, count + "nd warning" + machineName + " has opened " + fileName + " from path " + filePath)); 
     if (machineName == pc1.Text) 
     { 
      pc1.Image = Image.FromFile("../../secondwarning.png"); 
     } 
     else if (machineName == pc2.Text) 
     { 
      pc2.Image = Image.FromFile("../../secondwarning.png"); 
     } 
     else 
     { 
      pc3.Image = Image.FromFile("../../secondwarning.png"); 
     } 
    } 
    else if (count == 3) 
    { 
     listBox1.Items.Add(new MyListBoxItem(Color.Firebrick, count + "rd warning" + machineName + " has opened " + fileName + " from path " + filePath)); 
     if (machineName == pc1.Text) 
     { 
      pc1.Image = Image.FromFile("../../thirdwarning.png"); 
     } 
     else if (machineName == pc2.Text) 
     { 
      pc2.Image = Image.FromFile("../../thirdwarning.png"); 
     } 
     else 
     { 
      pc3.Image = Image.FromFile("../../thirdwarning.png"); 
     } 
    } 
    else { listBox1.Items.Add(new MyListBoxItem(Color.Gray, count + "th warning" + machineName + " has opened " + inputfile + " from path " + filePath)); } 
    conDataBase.Close(); 
+0

所需的參數,你忘了設置@machineName參數上的命令字符串? – Sami

回答

1

你已經忘了補充由cmd1Database命令

---- 
string count1 = @"SELECT COUNT(hostName) FROM chtbuster.processtable 
        WHERE hostName = @machineName; "; 
MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase); 
cmd1Database.Parameters.AddWithValue("@machineName", machineName); 
long count = (long)cmd1Database.ExecuteScalar(); 
---- 
+0

嗯謝謝你的快速響應先生!現在就工作了! – charlie9495

+1

與您的問題沒有嚴格關係,但值得一讀[我們可以停止使用AddWithValue了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue - 已經/) – Steve

+0

我會研究它的先生。再一次,謝謝 – charlie9495

相關問題