public string getTypenameById(int TypeId)
        {
            try
            {
                string sql = "select TypeName from RoomType where TypeId='" + TypeId + "'";
                SqlCommand scm = new SqlCommand(sql, con.getCon());
                con.getCon().Open();
                SqlDataReader sdr = scm.ExecuteReader();                string typename = string.Empty;
                while (sdr.Read())
                {
                    typename = (string)sdr["RoomType"];
                }
                return typename;
                sdr.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally {
                con.getCon().Close();
            }
        }

解决方案 »

  1.   

    con.getCon().Open(); 
    这句
      

  2.   

    看了,程序应该没有问题,感觉好像是con.getCon()有问题,看这里面的字条串是否有效
      

  3.   

    这个是DBHelper中的方法public class DBHelper
        {
            public SqlConnection getCon()
            {
                string connectionstring = "server=.;trusted_connection=true;database=HotelManage";
                SqlConnection con = new SqlConnection(connectionstring);
                return con;
            }
        }
      

  4.   

    问题解决了 
    在程序上又new了一遍
    sqlconnection 好了