如何从数据库中存储特定值并保存到cookie中

时间:2015-03-20 11:05:28

标签: c# asp.net ado.net

SqlCommand cmd = new SqlCommand("select folderlocation from fsfilestorage 
                                 where id='"+sessionid+"'", con);
SqlDataReader reader1 = cmd.ExecuteReader();
reader1.Read();
string folder = string.Empty;
folder = reader1.GetString(0);

1 个答案:

答案 0 :(得分:0)

使用SqlCommand.ExecuteScalar从db获取单个值并将其添加到Response.Cookies

var folder=cmd.ExecuteScalar().ToString();

HttpCookie myCookie = new HttpCookie("folder");
myCookie["FolderPath"] = folder;
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);