2014-02-14 14 views
-3

當我想檢查是否存在於數據庫條目中的程序給我一個錯誤:如果在數據庫中存在錯誤值C#檢查

Keyword not supported: 'datasource'

我的代碼:

public bool FindString(string myString) 
{ 
     SqlConnection connection = new SqlConnection(); 
     connection.ConnectionString = "datasource=localhost;port=3306;username=admin;password=admin"; 
     SqlCommand command = new SqlCommand(); 
     command.Connection = connection; 
     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "Create Procedure FindString(@MyString nvarchar(50)) as Begin Select * test.user Where Value = @MyString End"; 
     command.Parameters.AddWithValue("@MyString", myString); 

     try 
     { 
      connection.Open(); 
      SqlDataReader reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       return true; 
      } 
     } 
     catch (Exception ex) 
     { 
      System.Windows.Forms.MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      if (connection.State == ConnectionState.Open) 
       connection.Close(); 
     } 
     return false; 
    } 

而且代碼使用該類:

ReadData r = new ReadData(); 

if (r.FindString(textBox1.Text)) 
    MessageBox.Show("I Found it!"); 
else 
    MessageBox.Show("I can't Find it!"); 

回答

2

在ConnectionString配置的上下文中不存在關鍵字「datasource」。您應該使用「日源」作爲兩個獨立的詞來代替:

connection.ConnectionString = "data source=localhost;port=3306;username=admin;password=admin"; 
0

here:這是數據源在兩個分開的話

2

如果你正在使用MySQL:SqlConnection是SQL Server。改爲使用MySqlConnection

如果你還沒有MySQL的.Net連接器,你可以下載它from here

0

Look here我讓你寫的糟糕的connectionString。正如@bejger寫的,你需要寫「數據源」

相關問題