日期:2014-05-18  浏览次数:20842 次

OleDbDataAdapter构造函数返回值为null,紧急求助!
几个查询字符串我都在Access里试过了,能正常查询。。
第三个try a1.Fill(riCheng); 这里发现了异常,说a1是null
另外我一开始忘了写conn.close()就运行了,虽然后面补上了,但据说要成对出现。。不知这有没有影响。。

public class LinkData : INotifyPropertyChanged
  {
  public event PropertyChangedEventHandler PropertyChanged;
  private OleDbDataAdapter a1, a2;
  private OleDbConnection conn;
  private DataTable riCheng, daiBan;
  public LinkData()
  {
  string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Environment.CurrentDirectory
  + @"\deskdata\DesktopData.mdb;";
  conn = new OleDbConnection(strCnn);
  string s1 = "SELECT * FROM RiChengData order by time,status desc,id";
  string s2 = "SELECT * FROM DaiBanData order by begindate,status desc,enddate,id";
  conn.Open();
  try { a1 = new OleDbDataAdapter(s1,conn); }
  catch (Exception e)
  {
  MessageBox.Show(e.ToString());
  }
   
  a2 = new OleDbDataAdapter(s2, conn);
  conn.Close();
  try { a1.Fill(riCheng); }
  catch (Exception e)
  {
  MessageBox.Show(e.ToString());
  }
  a2.Fill(daiBan);
  }

------解决方案--------------------
public class LinkData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private OleDbDataAdapter a1, a2;
private OleDbConnection conn;
private DataTable riCheng, daiBan;
public LinkData()
{
string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Environment.CurrentDirectory
+ @"\deskdata\DesktopData.mdb;";
conn = new OleDbConnection(strCnn);
string s1 = "SELECT * FROM RiChengData order by time,status desc,id";
string s2 = "SELECT * FROM DaiBanData order by begindate,status desc,enddate,id";

try { 
conn.Open();
a1 = new OleDbDataAdapter(s1,conn);
a1.Fill(riCheng);
a2 = new OleDbDataAdapter(s2, conn);
a2.Fill(daiBan);
conn.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
------解决方案--------------------
不是a1为null,可能是riCheng为null,先new一个DataTable试试。另外一个也是。