C# System.FormatException:@name:as-输入字符串的格式不正确

C# System.FormatException:@name:as-输入字符串的格式不正确,c#,sql-server-ce,C#,Sql Server Ce,我正在C#VS 2010中开发一个应用程序 我正在使用CE数据库来解决这个问题。我从数据库中搜索字符串的代码是 string con = @"Data Source=|DataDirectory|\Database\Acadamy.sdf;Persist Security Info=False"; string cmd = "SELECT sid AS [Student ID], sname AS [Student Name], contact AS [Contact No] FROM Stu

我正在C#VS 2010中开发一个应用程序

我正在使用CE数据库来解决这个问题。我从数据库中搜索字符串的代码是

string con = @"Data Source=|DataDirectory|\Database\Acadamy.sdf;Persist Security Info=False";
string cmd = "SELECT  sid AS [Student ID], sname AS [Student Name], contact AS [Contact No] FROM Student WHERE sname LIKE '%'+ @name +'%'";
var dt = new DataTable();
using (var a = new SqlCeDataAdapter())
{
 try
  {
   a.SelectCommand = new SqlCeCommand();
   a.SelectCommand.Connection = new SqlCeConnection(con);
   a.SelectCommand.CommandType = CommandType.Text;
   a.SelectCommand.CommandText = cmd;
   a.SelectCommand.Parameters.AddWithValue("@name", textBox1.Text);
   a.Fill(dt);
   dataGridView1.DataSource = dt;
  }
  catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
}
当我输入任何字符串(如
as
时,我收到错误
System.FormatException:@name:as-输入字符串的格式不正确。


什么是无法修复的错误。

从sql命令文本中删除引号。
参数对于帮助避免这种混乱也是非常宝贵的

将通配符字符添加到参数值

string cmd = "SELECT  sid AS [Student ID], sname AS [Student Name], " + 
            "contact AS [Contact No] FROM Student WHERE sname LIKE @name";
....
a.SelectCommand.Parameters.AddWithValue("@name", "%" + textBox1.Text + "%") ;

哇……太完美了。它在工作。非常感谢你。