J
Jorgen D.
I'm using SqlCeDB and having troubels with prepared
commands and the data reader.
The first call to the Read method dosen't return a result,
but recreating the reader, and call the read method again
works as expected.
Here is a sample, which will display 2 messagboxes. The
first eith "Not Found" and the Second with "Found...".
What am I dooing wrong ?
Regards Joergen D.
SqlCeConnection cn = new SqlCeConnection(@"Data Source=\My
Documents\data.sdf");
cn.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT itemname from
Stocktable where itemnumber = ?",cn);
cmd.Parameters.Add("Ean",SqlDbType.NVarChar);
cmd.Prepare();
SqlCeDataReader dtr;
// 1st try
cmd.Parameters[0].Value="5703919000189";
dtr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dtr.Read())
{
string s = dtr.GetString(0);
MessageBox.Show("1. try - Found: "+s);
}
else
{
MessageBox.Show("1. try - Not Found");
}
dtr.Close();
// 2nd try
cmd.Parameters[0].Value="5703919000189";
dtr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dtr.Read())
{
string s = dtr.GetString(0);
MessageBox.Show("2. try - Found: "+s);
}
else
{
MessageBox.Show("2. try - Not Found");
}
dtr.Close();
cn.Close();
commands and the data reader.
The first call to the Read method dosen't return a result,
but recreating the reader, and call the read method again
works as expected.
Here is a sample, which will display 2 messagboxes. The
first eith "Not Found" and the Second with "Found...".
What am I dooing wrong ?
Regards Joergen D.
SqlCeConnection cn = new SqlCeConnection(@"Data Source=\My
Documents\data.sdf");
cn.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT itemname from
Stocktable where itemnumber = ?",cn);
cmd.Parameters.Add("Ean",SqlDbType.NVarChar);
cmd.Prepare();
SqlCeDataReader dtr;
// 1st try
cmd.Parameters[0].Value="5703919000189";
dtr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dtr.Read())
{
string s = dtr.GetString(0);
MessageBox.Show("1. try - Found: "+s);
}
else
{
MessageBox.Show("1. try - Not Found");
}
dtr.Close();
// 2nd try
cmd.Parameters[0].Value="5703919000189";
dtr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dtr.Read())
{
string s = dtr.GetString(0);
MessageBox.Show("2. try - Found: "+s);
}
else
{
MessageBox.Show("2. try - Not Found");
}
dtr.Close();
cn.Close();