2014-01-07 19 views
0

我有一個簡單的查詢來更新用戶信息,但我得到的錯誤指出'初始化字符串的格式不符合規範在索引33',它似乎突出這一特定代碼然而Connection.Close();林不知道爲什麼,這裏是完整的代碼:初始化字符串的格式不符合指數33

public void AddNewUser() 
{ 
    string filePath; 
    try 
    { 
     filePath = (Application.StartupPath + ("\\" + DBFile)); 
     connection = new System.Data.OleDb.OleDbConnection((ConnectionString + filePath)); 
     connection.Open(); 
     System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(); 
     command.Connection = connection; 
     // ---set the user's particulars in the table--- 
     string sql = ("UPDATE enroll SET SSN=\'" 
         + (txtSSN.Text + ("\', " + ("FirstName=\'" 
         + (txtFirstName.Text + ("\', " + ("LastName=\'" 
         + (txtLastName.Text + ("\' " 
         + (" WHERE ID=" + _UserID)))))))))); 
     command.CommandText = sql; 
     command.ExecuteNonQuery(); 
     MessageBox.Show("Student added successfully!", "Registered"); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString(), "Error"); 
    } 
    finally 
    { 
     connection.Close(); 
    } 
} 

編輯:

以下是文件路徑:

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Users\\Zack\\My Documents\\Test\\Database.mdb"; 

const string DBFile = "C:\\Users\\Zack\\My Documents\\Test\\Database.mdb"; 
+6

您有一個SQL注入漏洞。 – SLaks

+2

您的連接字符串sytnax不正確。查看:http://stackoverflow.com/questions/8243008/format-of-the-initialization-string-does-not-conform-to-specification-starting-a或發佈什麼'filePath'評估的樣本。 – Nico

+0

@Nico我編輯了這個問題並添加了文件路徑 – Zack

回答

0

命令文本是錯誤的,你應該使用parametirized查詢,這裏是正確的版本:

command.CommandText = "UPDATE enroll SET SSN= @ssn, FirstName = @fname, LastName = @lastName WHERE ID = @id"; 
command.Parameters.AddWithValue("@ssn", txtSSN.Text); 
command.Parameters.AddWithValue("@fname", txtFirstName.Text); 
command.Parameters.AddWithValue("@lastName", txtLastName.Text); 
command.Parameters.AddWithValue("@id", _UserID); 

和連接字符串:

string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Zack\My Documents\Test\Database.mdb'"; 
+0

我試過這個,但是我得到相同的錯誤代碼 – Zack

+0

檢查您的連接字符串 –

0

扎克,

有相當數量的問題與此代碼。主要是如果你要運行這個(就像SLacks說的那樣),你對sql注入攻擊是開放的。 (閱讀它)。

首先,您的連接字符串(基於您的代碼)運行時將是。

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Users\\Zack\\My Documents\\Test\\Database.mdb\\C:\\Users\\Zack\\My Documents\\Test\\bin\Debug\\C:\\Users\\Zack\\My Documents\\Test\\Database.mdb 

那麼這是一個猜測。你應該使用以下內容(注意你的路徑是硬編碼的)。

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\""; 

const string DBFile = "Database.mdb"; 
//... 
var connection = new System.Data.OleDb.OleDbConnection(ConnectionString) 

如果你想讓你的連接字符串動態到路徑試試這個。

string conString = string.Format(ConnectionString, Path.Combine(Application.StartupPath, DBFile)); 
var connection = new System.Data.OleDb.OleDbConnection(conString); 

這應該正確設置您的連接字符串給您的應用程序啓動。現在你可能會發現它更適合執行程序集路徑而不是應用程序啓動(你的調用)。

接下來你的疑問是一團糟。我已經清理它,以使用參數化查詢而不是結果代碼。 (注意這還沒有經過測試)。

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\""; 

const string DBFile = "Database.mdb"; 
public void AddNewUser() 
{ 
    string conString = string.Format(ConnectionString, Path.Combine(Application.StartupPath, DBFile)); 
    using (var connection = new System.Data.OleDb.OleDbConnection(conString)) 
    { 
     try 
     { 
      string sql = "UPDATE enroll SET [email protected], [email protected], [email protected] WHERE [email protected]"; 
      System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(sql, connection); 
      command.Parameters.AddWithValue("@ssn", txtSSN.Text); 
      command.Parameters.AddWithValue("@firstName", txtFirstName.Text); 
      command.Parameters.AddWithValue("@lastName", txtLastName.Text); 
      command.Parameters.AddWithValue("@userID", _UserID); 

      connection.Open(); 
      command.ExecuteNonQuery(); 
      MessageBox.Show("Student added successfully!", "Registered"); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString(), "Error"); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
} 

編輯:

我創建了一個測試實驗室爲上面的代碼和所有的正確運行。如果您有任何問題,請告訴我。

乾杯。

+0

乾杯的幫助,我試過了,但它突出了'connection.Close();'並說NullReferenceException是未處理的? – Zack

+0

@Zack你確信你的所有值都已設置好了嗎?如果您將調試器設置在第一行並檢查每個變量。你也可以發佈完整的錯誤消息,包括stacktrace?這將告訴我們確切的問題在哪裏。 – Nico

+0

我必須引用我的數據源錯誤,因爲當我刪除數據源時,它工作正常100%,但只要我嘗試並引用我的數據庫連接字符串數據源我得到相同的錯誤 – Zack

相關問題