![](/img/trans.png)
[英]System.FormatException:Input string was not in a correct format
[英]System.FormatException: @name : as - Input string was not in a correct format
我正在使用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 - Input string was not in a correct format.
什么是錯誤無法修復。
從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 + "%") ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.