简体   繁体   English

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

[英]System.FormatException: @name : as - Input string was not in a correct format

I am developing an application in C# VS 2010. 我正在使用C#VS 2010开发一个应用程序。

I am using CE database to this. 我正在使用CE数据库。 My code to search a string from database is 我从数据库中搜索字符串的代码是

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);
   }
}

When I input any string like as I am getting error System.FormatException: @name : as - Input string was not in a correct format. 当我输入像任何字符串as我收到错误System.FormatException: @name : as - Input string was not in a correct format.

What will be the error unable to fix. 什么是错误无法修复。

Remove the quoting from the sql command text. 从sql命令文本中删除引用。
Parameters are invaluable also for helping to avoid that confusion. 参数对于帮助避免混淆也是非常宝贵的。

Add the wildcard chars to the parameter value.... 将通配符字符添加到参数值....

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 + "%") ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:输入字符串的格式不正确 - System.FormatException:Input string was not in a correct format System.FormatException:输入字符串的格式不正确 - System.FormatException: Input string was not in a correct format C#System.FormatException:输入字符串的格式不正确 - C# System.FormatException: Input string was not in a correct format 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format 错误:System.FormatException:输入字符串的格式不正确 - ERROR : System.FormatException: Input string was not in the correct format System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException: '输入字符串的格式不正确。' - System.FormatException: 'Input string was not in a correct format.' System.FormatException:输入字符串的格式不正确,为十进制 - System.FormatException: Input string was not in a correct format for decimal System.FormatException: '输入字符串的格式不正确。' 数据网格 - System.FormatException: 'Input string was not in a correct format.' data grid System.FormatException:'输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM