public static object select<T>(string biao,string ziduan, string tiaojian)//查询某个记录的某个字段
        {
            object obj;
            
            try
            {
                conn.Open();
                string str = "SELECT " + ziduan + " FROM " + biao + " where " + ziduan + "='" + tiaojian + "'";
                SqlCommand cmd = new SqlCommand(str, conn);
                obj = cmd.ExecuteScalar();
                if (obj != null || obj != DBNull.Value)
                {
                    if (T is string)
                    {
                        T ts;
                        ts = obj.ToString();
                    }
                    else if (T is double)
                    {
                        T ts;
                        ts = double.Parse(obj.ToString());
                    }                }
                else
                {
                    if (T is string)
                    {
                        T ts;
                        ts = "";
                    }
                    else if (T is double)
                    {
                        T ts;
                        ts = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();
            return ts;
        }        public static bool cunzai<T>(string biao, string ziduan, string tiaojian)//检查是否已存在该记录
        {
            if (T is string)
            {
                string te = "";
                te = DAL.select<string>(biao, ziduan, tiaojian);
                if (te != "")
                    return true;
                else
                    return false;
            }
            else if (T is double)
            {
                double te = 0;
                te = DAL.select<double>(biao, ziduan, tiaojian);
                if (te != 0)
                    return true;
                else
                    return false;
            }
        }帮忙改一改................目的是搜索数据库里面的一个值得是否存在,无论这个值是string还是double都可以使用的函数.

解决方案 »

  1.   

    public static bool cunzai<T>(string biao, string ziduan,  T tiaojian)//检查是否已存在该记录
            {                T te = DAL.select<T>(biao, ziduan, tiaojian);
    return te != null
            }
    不能通过是空字符串或者是0来判断存在,因为数据库里面也可能的确是空字符串或者0,DAL.select返回值为null应该表示不存在
      

  2.   

    public static T select<T>(string biao,string ziduan, string tiaojian)
    {}二楼的代码会出错, 如果T 不限定 where T : object. 那么是不能用T == null在判断的。 因为T有可能是值类型。
      

  3.   


    public static T RequestValue<T>(string ValueName)
            {
                HttpContext rq = HttpContext.Current;
                T TempValue;            if (rq.Request.QueryString[ValueName] != null)
                {
                    TempValue = (T)Convert.ChangeType(rq.Request.QueryString[ValueName],typeof(T));
                }
                else
                {
                    TempValue =default(T);
                }            return TempValue;
            }
      

  4.   


            public static T select<T>(string biao,string ziduan, T tiaojian)//查询某个记录的某个string字段
            {
                object obj;
                T ts = default(T);
                try
                {
                    conn.Open();
                    string str = "SELECT " + ziduan + " FROM " + biao + " where " + ziduan + "='" + tiaojian + "'";
                    SqlCommand cmd = new SqlCommand(str, conn);
                    obj = cmd.ExecuteScalar();
                    if (obj != null || obj != DBNull.Value)
                    {
                        ts = (T)Convert.ChangeType(obj, typeof(T));
                    }
                    else
                    {
                        ts = default(T);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
                return ts;
            }这样改对不???我是楼主~
      

  5.   

    泛型处理这个问题实在是大材小用,直接判断读出数据是否为null不是搞定了吗?
      

  6.   

    直接查询出该值,判断他是否为null,做项目都是这么做的嘛~!大家说是么.
      

  7.   

    但是,这个select函数不单是用来判断是否存在,而是可以重用在查询方面的哇
      

  8.   

    你不就是为了返回string或double类型么
    那你直接返回object类型就可以了
    在外部调用时判断是否是null并且转换为你要的string或double