2014-02-14 15 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写的,你需要写“数据源”

相关问题