public void getNews(string nid)
    {
        newsDB getNews = new newsDB();
        SqlDataReader dr = getNews.getNews("select", nid);
        if (dr.Read())
        {
            title.Text = dr["nid"].ToString();
        }
        title.Text = "郁闷,还是没成功!";
    }
--------------------------------------------------------
用这个函数来取值,却没有值被取到..

解决方案 »

  1.   

    楼主的代码好乱呀,
    为什么要放两个try catch?
      

  2.   

    给你两个方法,你自己去改改吧private static string ConnectionString
            {
                get
                {
                    return ConfigurationManager.ConnectionStrings["SqlServerConnection"].ToString();
                }
            }
            private DataAccess()
            {        }        private static SqlConnection DBConnection
            {
                get
                {
                    SqlConnection conn = null;
                    try
                    {
                        conn = new SqlConnection(ConnectionString);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error:ExcuteQuery()-->" + ex.Message);
                    }
                    return conn;
                }
            }        public static SqlDataReader ExcuteQuery(string StrSQL)
            {
                SqlCommand sqlcmd = new SqlCommand(StrSQL, DBConnection);
                try
                {
                    sqlcmd.Connection.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error:ExcuteQuery()-->" + ex.Message);
                }
                finally
                {
                    sqlcmd.Dispose();
                }
                return sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
            }        public static SqlDataReader ExcuteQuery(SqlCommand cmd)
            {
                try
                {
                    cmd.Connection = DBConnection;
                    cmd.Connection.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error:ExcuteQuery()-->" + ex.Message);
                }
                finally
                {
                    cmd.Dispose();
                }
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
      

  3.   

    好象是cmd.ExecuteReader(CommandBehavior.CloseConnection)没有返回值啊..怎么弄啊
      

  4.   

    我怀疑问题出在SqlConnection身上,他是局部变量!也就是说在在完成这个函数以后就释放内存!
    SqlDataReader 是链接状态下的,没有了链接就
    真正的还没试过
    只是我的假设!