繁体   English   中英

ExecuteReader:CommandText属性未初始化

[英]ExecuteReader: CommandText property not initialized

public DataTable Liste()
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-JHLF03K\\SQLEXPRESS;Initial Catalog=OtelWebSite;Integrated Security=True");
    string sql = "";
    SqlDataAdapter dap = new SqlDataAdapter(sql, con);  
    DataTable table = new DataTable();
    con.Open();
    dap.Fill(table);

    sql += "SELECT ";
    sql += "O.Id, ";
    sql += "O.OdaTurId,";
    sql += "T.Ad AS OdaTur, ";
    sql += "O.Ad, ";
    sql += "O.KatNo, ";
    sql += "O.Aciklama, ";
    sql += "K.Tanim AS Durum ";
    sql += "FROM Oda O, Kod K,OdaTur T  ";
    sql += "WHERE O.Durum = K.Kod ";
    sql += "AND T.Id = O.OdaTurId ";
    sql += "ORDER BY O.Id,O.OdaTurId";



    con.Close();
    return table;
}

我得到一个错误

ExecuteReader:CommandText属性未初始化

在这行代码上:

dap.Fill(table);

您无法执行并清空sql查询。 将您的sql查询创建移动到创建DataAdapter的行之前的顶部

public DataTable Liste()
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-JHLF03K\\SQLEXPRESS;Initial Catalog=OtelWebSite;Integrated Security=True");
    string sql = "";

    sql += "SELECT ";
    sql += "O.Id, ";
    sql += "O.OdaTurId,";
    sql += "T.Ad AS OdaTur, ";
    sql += "O.Ad, ";
    sql += "O.KatNo, ";
    sql += "O.Aciklama, ";
    sql += "K.Tanim AS Durum ";
    sql += "FROM Oda O, Kod K,OdaTur T  ";
    sql += "WHERE O.Durum = K.Kod ";
    sql += "AND T.Id = O.OdaTurId ";
    sql += "ORDER BY O.Id,O.OdaTurId";

    SqlDataAdapter dap = new SqlDataAdapter(sql, con);  
    DataTable table = new DataTable();
    con.Open();
    dap.Fill(table);

    con.Close();
    return table;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM