我为 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));

关于c# - .Net 紧凑型框架 SqlClient 异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23494941/

10-11 23:05
查看更多