2014-05-22 101 views
0

我嘗試將插入值連接到數據庫時遇到標題錯誤。我調試的錯誤出在:使用在System.Data.dll中發生未處理的類型爲「System.ArgumentException」的異常

(VAR康恩=新的MySqlConnection(myConString))

以下是錯誤的VS2013:

System.ArgumentException了未處理的HResult = - 2147024809
消息=初始化字符串的格式不符合從索引153開始的 規範。Source = System.Data
StackTrace: 在System.Data.Common.DbConnectionOptions.GetKeyValuePair(字符串 的connectionString,的Int32 currentPosition,StringBuilder的緩衝液,布爾 useOdbcRules,字符串&鍵名,字符串&鍵值) 在System.Data.Common.DbConnectionOptions.ParseInternal(哈希表 parsetable,串的connectionString,布爾buildChain,哈希表 同義詞,布爾firstKey) 在System.Data.Common.DbConnectionOptions..ctor(串的connectionString,哈希表同義詞,布爾useOdbcRules) 在System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(字符串 值) at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr) 在MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串 值) 在MySql.Data.MySqlClient.MySqlConnection..ctor(字符串的connectionString) 在MD5_Loader.MainForm.btn_Upload_Click(對象發件人,EventArgs e)在d:\ xxxx \ MD5 Loader \ MD5 Loader \ MainForm.cs:line 43 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message & m,MouseButtons button,Int32 clicks) at System.Windows.Forms.Control.WndProc(Message &米) 在System.Windows.Forms.ButtonBase.WndProc(消息&米) 在System.Windows.Forms.Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows .Forms.UnsafeNativeMethods.DispatchMessageW(MSG & msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,的Int32原因,的Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32 原因,ApplicationContext的上下文) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32 原因,ApplicationContext的上下文) at System.Windows.Forms.Application.Run(Form mainForm) at MD5_Loader.Program.Main()in d:\ Dropbox \ Bots \ World of Warcraft \ MD5Loader \ MD5 Loader \ MD5 Loader \ Program。CS:行19
的InnerException:

下面是代碼:

using System; 
using System.Data; 
using System.IO; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 

namespace MD5_Loader 
{ 
    public partial class MainForm : Form 
    { 
     private const string Server = "xxx.xxx.xxx.xxx"; 
     private const string Port = "3306"; 
     private const string Md5DataBase = "database"; 
     private const string Md5Table= "Builds"; 
     private const string DbUser = "admin"; 
     private const string DbPass = "admin"; 
     private string _fileName = ""; 
     private string _md5 = ""; 

     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     private void btn_Upload_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       //Variable for MySQL server Connection 
       const string myConString = "Server=" + Server + ";" + 
              "PORT=" + Port + ";" + 
              "DATABASE=" + Md5DataBase + ";" + 
              "Persist Security Info=No;" + 
              "Encrypt=True;" + 
              "SslMode=Required;" + 
              "username=" + DbUser + ";" + 
              "password=" + DbPass + ";"; 
       MessageBox.Show(tbx_Revision.Text + Environment.NewLine + 
           CheckMd5(tbx_FileName.Text) + Environment.NewLine + 
             Path.GetFileNameWithoutExtension(tbx_FileName.Text)); 
       using (var conn = new MySqlConnection(myConString)) 
       { 
        using (var cmd = new MySqlCommand(
         "INSERT INTO " + Md5Table + 
         "(Revision, CheckSum, Product) " + 
         "VALUES (@Revision, @CheckSum, @Product)", 
         conn)) 
        { 
         cmd.CommandType = CommandType.Text; 
         cmd.Parameters.AddWithValue("@Revision", tbx_Revision.Text); 
         cmd.Parameters.AddWithValue("@CheckSum", CheckMd5(tbx_FileName.Text)); 
         cmd.Parameters.AddWithValue("@Product", Path.GetFileNameWithoutExtension(tbx_FileName.Text)); 
         conn.Open(); 
         try 
         { 
          cmd.ExecuteNonQuery(); 
         } 
         catch (Exception ex) 
         { 
          MessageBox.Show(ex.ToString()); 
         } 
         finally 
         { 
          cmd.Dispose(); 
         } 
        } 
        conn.Close(); 
        conn.Dispose(); 
       } 
      } 
      catch (MySqlException err) 
      { 
       MessageBox.Show("Error: " + err); 
      } 
     } 
    } 
} 
+0

連接字符串是字符指數153無效我建議你看那個連接字符串的內容,並確定哪些是在該位置。請確保您符合www.connectionstrings.com建議的內容。 – jmcilhinney

+0

@JonH當你的代碼產生上述異常時,'myConString'的值是什麼?它可能是特定於您正在使用的參數的內容。特別是在字符153處。如果要編輯字符串以刪除敏感數據,請突出顯示字符153在哪裏。 –

+0

@Alex:Server = xxx.xxx.xxx.xxx; PORT = 3306; DATABASE = database; Persist Security Info = No; Encrypt = True; SslMode = Required; username = admin; password = admin; 編輯:我能夠Navicat與憑據。我也加倍檢查並剪切並粘貼,以確保它不是那樣。 –

回答

0

答案來自於密碼的形式。生成的密碼來自託管網站包含一個「;」在密碼中。這在連接字符串中造成了錯誤。我感謝大家的幫助。有時必須清除問題禁止正確的答案。我感謝你的時間。

-2

添加以下代碼在web.config中

<appSettings> 
    <add key="ConSecMode" value="OPEN"/> 
    </appSettings> 
相關問題