如何解决CommandText属性尚未初始化? [英] How to solve CommandText property has not been initialized?
问题描述
嗨我收到此错误" 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屋!