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; } }标签:初始化,MessageBox,string,adp,Show,SelectCommand,DT,ds,Fill From: https://www.cnblogs.com/Dongmy/p/16953988.html