如何解决CommandText属性尚未初始化? [英] How to solve CommandText property has not been initialized?

查看:2161
本文介绍了如何解决CommandText属性尚未初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我收到此错误"  CommandText属性尚未初始化"


代码如下:


  private DataTable GetData(string p)

        {

            DataTable dt = new DataTable();

            string constr = ConfigurationManager.ConnectionStrings [" ApplicationServices"]。ConnectionString;

   使用(SqlConnection con = new SqlConnection(constr))

    {

        using(SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query))

        {

           使用(SqlDataAdapter sda = new SqlDataAdapter())

            {

                cmd.CommandType = CommandType.Text;

                cmd.Connection = con;

                sda.SelectCommand = cmd;

                sda.Fill(dt);

            }¥b $ b        }¥b $ b       返回dt;

    } b $ b}



任何人都知道帮助我解决问题。





Divya Diya




解决方案

异常消息提示您在调用Fill方法之前需要将SELECT查询分配给cmd.CommandText属性。确保查询变量的值不为空或为空。






hi am getting this error " CommandText property has not been initialized"

The code is below:

 private DataTable GetData(string p)
        {
            DataTable dt = new DataTable();
            string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        return dt;
    }
}

Anyone Know help me to solve the issue.


Divya Diya


解决方案

The exception message is giving you a hint that a SELECT query needs to be assigned to the cmd.CommandText property before invoking the Fill method. Make sure the value of the query variable is not null or empty.



这篇关于如何解决CommandText属性尚未初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆