在调用“Fill”前,SelectCommand 属性尚未初始化
https://www.cnblogs.com/wangfeihu/p/5636611.html
这个报错是因为连接问题,查到的如下,我这边是因为版本不一致导致的连接问题。
是因为少写了一行代码:
private readonly string strConnection = System.Configuration.ConfigurationManager.AppSettings["ConnStr"].ToString();
主要还是连接的问题
private void Form1_Load(object sender, EventArgs e) { string a = "2"; string sql = " select Date from T_BasicData where userid = '" + a + "'"; //DataTable DT = Classbase.pubfun.GetSercherGridSource(sql); DataTable DT = GetSercherGridSource(sql); string b = DT.Rows[0][0].ToString(); MessageBox.Show(b); }
public DataTable GetSercherGridSource(string cmdText) { DataTable DT = null; SqlConnection conn = new SqlConnection(strConnection); SqlCommand cmd=conn.CreateCommand(); SqlDataAdapter adp=new SqlDataAdapter(); adp.SelectCommand = cmd; DataSet ds =new DataSet(); cmd.CommandText=cmdText; adp = new SqlDataAdapter(); try { conn.Open() ; adp.SelectCommand = cmd;//看看有没有这句代码 adp.Fill(ds) ; if (ds != null) { DT = ds.Tables[0]; } return DT; } catch (Exception e) { //MessageBox.Show("错误"); //MessageBox.Show(string.Format("异常!以下为数据库提示详细错误信息:{0}{1}", Environment.NewLine, cct.CUR_ERROR), "提示"); MessageBox.Show(string.Format("异常!以下为数据库提示详细错误信息:{0}{1}", Environment.NewLine, e.Message), "提示"); //MessageBox.Show(e.Message); return DT; } }