试试这个:
string query = "...(whatever you need).....";
using(SqlCeConnection conn = new SqlCeConnection(connectionString))
using(SqlCeCommand cmd = new SqlCeCommand(query, conn))
{
// just add parameters directly to SqlCeCommand object ....
cmd.Parameters.Add("username", Username);
cmd.Parameters.Add("password", Password);
conn.Open();
cmd.ExecuteNonQuery(); // or whatever you need to do
conn.Close();
}
如果您必须预先单独设置参数,那么您需要执行以下操作(因为您不能直接使用SqlCeParameterCollection
):
List<SqlCeParameters> parameters = new List<SqlCeParameters>();
parameters.Add(new SqlCeParameter(.....));
parameters.Add(new SqlCeParameter(.....));
string query = "...(whatever you need).....";
using(SqlCeConnection conn = new SqlCeConnection(connectionString))
using(SqlCeCommand cmd = new SqlCeCommand(query, conn))
{
// add all parameters from the list - casting to an array
cmd.Parameters.AddRange(parameters.ToArray());
conn.Open();
cmd.ExecuteNonQuery(); // or whatever you need to do
conn.Close();
}