. net精简框架SqlClient异常



我为Windows CE 5.0设备开发,我正在尝试连接到远程SQL Server。但是如果我使用带参数的命令,我会得到异常:

{System.Data.SqlClient.SqlException: SqlException
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Test.MainForm.TestConnection()
...
}
代码:

SqlConnection conn = new SqlConnection(connectionString);  
conn.Open();  
SqlCommand cmd = conn.CreateCommand();  
cmd.CommandText = "select * from table_name where id = @id";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("id", 1));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];

不添加参数的命令,它工作得很好。请告诉我原因是什么?(我使用VS2008, .net紧凑框架3.5)

在参数名称前加上'@',如

cmd.Parameters.Add(new SqlCeParameter("@id", 1));

相关内容

  • 没有找到相关文章

最新更新