我正在使用访问数据库,当我尝试执行此代码时,它会得到“行/列没有数据。我在Microsoft Access中执行了sql代码,它正常工作。
OleDbCommand cmd1 = new OleDbCommand("SELECT SUM (adet) AS gToplam from stok_isl WHERE stok_id = @veri and tur = 'GİRİŞ'", conn);
cmd1.Parameters.AddWithValue("@veri", combo);
OleDbDataReader dr1 = cmd1.ExecuteReader();
OleDbCommand cmd2 = new OleDbCommand("SELECT SUM (adet) AS cToplam from stok_isl WHERE stok_id = @veri and tur = 'ÇIKIŞ'", conn);
cmd2.Parameters.AddWithValue("@veri", combo);
OleDbDataReader dr2 = cmd2.ExecuteReader();
Sonuc = Convert.ToDouble(dr1["gToplam"]) - Convert.ToDouble(dr2["cToplam"]);
MessageBox.Show(Sonuc.ToString());
答案 0 :(得分:1)
由于您只检索一个号码,ExecuteScalar似乎是更适合您的选择:
...
double gToplam = (double)cmd1.ExecuteScalar();
...
double cToplam = (double)cmd2.ExecuteScalar();
Sonuc = gToplam - cToplam;
然而,当然你还可以使用ExecuteReader
。问题是您没有调用Read
(请参阅this page作为使用示例)方法来读取结果集的第一行:
...
OleDbDataReader dr1 = cmd1.ExecuteReader();
dr1.Read();
...
OleDbDataReader dr2 = cmd2.ExecuteReader();
dr2.Read();
请注意,这两个代码示例都不会检查查询返回空结果的情况。