首页 > 其他分享 >在调用“Fill”前,SelectCommand 属性尚未初始化

在调用“Fill”前,SelectCommand 属性尚未初始化

时间:2022-12-06 00:22:19浏览次数:44  
标签:初始化 MessageBox string adp Show SelectCommand DT ds Fill

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

相关文章