这是生成GUID的代码.我测试了是正确的可以生成..        
private static string shoppingCartId
        {
            get
            {
                HttpContext current = HttpContext.Current;
                string str = "";
                object obj2 = current.Session["bookShop_CartID"];
                if (obj2 != null)
                {
                    str = obj2.ToString();
                }
                if (str != null)
                {
                    return str;
                }
                if (current.Request.Cookies["bookShop_CartID"] != null)
                {
                    str = current.Request.Cookies["bookShop_CartID"].Value;
                    current.Session["bookShop_CartID"] = str;
                    return str;
                }
                else
                {
                    str = Guid.NewGuid().ToString();
                    HttpCookie cookie = new HttpCookie("bookShop_CartID", str.ToString());
                    int cartPersistDays = configuration.CartPersistDays;
                    DateTime now = DateTime.Now;
                    TimeSpan span = new TimeSpan(cartPersistDays, 0, 0, 0);
                    DateTime time2 = now.Add(span);
                    cookie.Expires = time2;
                    current.Response.Cookies.Add(cookie);
                    current.Session["bookShop_CartID"] = str;
                    return str.ToString();
                }
            }
        }
我是这样调用的
            SqlCommand command = DataAccess.CreateCommand();
            command.CommandText = "ShoppingCartAddItem";
            SqlParameter parameter = command.CreateParameter();
            parameter.ParameterName = "@cartID";
            parameter.Value = shoppingCartId;
            parameter.SqlDbType = SqlDbType.Char;
            parameter.Size = 0x24;
            command.Parameters.Add(parameter);
            parameter = command.CreateParameter();
            parameter.ParameterName = "@bookID";
            parameter.Value = bookID;
            parameter.SqlDbType = SqlDbType.Int;
            command.Parameters.Add(parameter);
为什么GUID没有存到数据库里呢...数据库里的那个字段是空的什么也没有,请帮忙

解决方案 »

  1.   

    parameter.SqlDbType = SqlDbType.UniqueIdentifier; 
      

  2.   

    好像没有说 "cartID" 一定是唯一约束吧?
      

  3.   

    补充一下我数据库里的那个字段是设为char 36的..跟这个有没有关系
      

  4.   

    晕.System.Guid.NewGuid()
    不就可以产生一个guid吗......char 36当然没有关系,你只要赋一个字符串类型就可以了.我的看法是最后将这一列设置唯一约束.
      

  5.   

    System.Guid.NewGuid()  在Replace一把就可以成36位字符