如何在数据库中存储cookie? [英] How to store a cookie in a database?

查看:107
本文介绍了如何在数据库中存储cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello People,



我的数据库包含名为Name and Contact的2列。我想在两个列中存储cookie。继承人我做了什么:





DATASOURCE:

Hello People,

I have a data base containing 2 columns called Name and Contact. I want to store cookies in both the columns. Heres what i did :


DATASOURCE:

<asp:SqlDataSource ID="Sql1" runat="server" ConnectionString="<%$ ConnectionStrings:contacts %>" OnSelected="selected"  SelectCommand="select * from contacts" InsertCommand="insert contacts (Name,Contact) values(@Name,@Contact)"><insertparameters><asp:CookieParameter Name="Contact" CookieName="Contact" /><asp:CookieParameter Name="Name" CookieName="Name" /></insertparameters>



- 我在插入参数中使用了cookie参数。这意味着当请求页面时,cookie需要存储在两列的数据库中。



FORMVIEW :(从用户那里获取输入)


- I have used cookie parameters in insert parameters. which means when ever the page is requested then a cookie needs to be stored in the database in both columns.

FORMVIEW: ( take input from user)

<asp:FormView ID="fv" runat="server"   DefaultMode="Insert" DataSourceID="sql1"><insertitemtemplate><asp:Label ID="l1" Text="Vote for" runat="server"/><asp:RadioButtonList DataValueField='<%#Bind("Name")%>' ID="rbl"  runat="server"><asp:ListItem Text="Varinder" Value="Varinder" Selected="true" /><asp:ListItem Text="Santosh" Value="Santosh" /> <asp:Button ID="b1" Text="Submit" runat="server" CommandName="Insert" />  </insertitemtemplate>



- 我使用了表单视图供用户输入。我刚刚尝试实现一种投票类型的机制,当选择一个单选按钮并按下一个按钮时,就会存储一个cookie来对照该人在数据库中的条目。



背后的代码是:


- I have used a form view for the user to input. I have just tried to implement a voting sort of a mechanism in which when a radio button is selected and a button is pressed, then a cookie will be stored against that person's entry in the database.

The code behind is :

protected void Page_Load(object sender, EventArgs e)
      {
          if(Request.Cookies["Name"]==null)
          {
              string identifier = Guid.NewGuid().ToString();
              HttpCookie namecoockie = new HttpCookie("Name",identifier);
              namecoockie.Expires = DateTime.MaxValue;
              Response.AppendCookie(namecoockie);


          }
      }



- 我想做的是当用户选择一个单选按钮然后将对应于该单选按钮选择的cookie存储到数据库中。

- 我是成功的,但问题是我的cookie只在列中重复,我希望在Name和Contact列中输入cookie。

- 我怀疑cookie是string类型。我的数据库的Name列也是string类型,但是Contact列的类型是int。

- 这是什么原因?因为在insert参数中我已经清楚地指定了要插入的内容。





谢谢,


- What i wanted to do is that when ever user selects a radio button then a cookie corresponding to that radio button selection will be stored into the data base.
- I am successfull in it, but the catch is that my cookie is being duplicated in only column, where as i want the entry of the cookie in both Name and Contact column.
- My doubt is that the cookie is of type string. The "Name" column of my database is also of type string, but "Contact" column is of type int.
- Is this the reason? Because in the insert parameter i have clearly specified what to insert where.


Thanks,

推荐答案

ConnectionStrings:contacts %> OnSelected = 选中 SelectCommand = 选择*来自联系人 InsertCommand = 插入联系人(姓名,联系人)值(@姓名,@联系人) > < insertparameters > < asp:CookieParameter 名称 = 联系人 CookieName = 联系人 / > < asp:CookieParameter 名称 = 名称 CookieName = 名称 / > < / insertparameters >
ConnectionStrings:contacts %>" OnSelected="selected" SelectCommand="select * from contacts" InsertCommand="insert contacts (Name,Contact) values(@Name,@Contact)"><insertparameters><asp:CookieParameter Name="Contact" CookieName="Contact" /><asp:CookieParameter Name="Name" CookieName="Name" /></insertparameters>



- 我在插入参数中使用了cookie参数。这意味着当请求页面时,cookie需要存储在两列的数据库中。



FORMVIEW :(从用户那里获取输入)


- I have used cookie parameters in insert parameters. which means when ever the page is requested then a cookie needs to be stored in the database in both columns.

FORMVIEW: ( take input from user)

<asp:FormView ID="fv" runat="server"   DefaultMode="Insert" DataSourceID="sql1"><insertitemtemplate><asp:Label ID="l1" Text="Vote for" runat="server"/><asp:RadioButtonList DataValueField='<%#Bind("Name")%>' ID="rbl"  runat="server"><asp:ListItem Text="Varinder" Value="Varinder" Selected="true" /><asp:ListItem Text="Santosh" Value="Santosh" /> <asp:Button ID="b1" Text="Submit" runat="server" CommandName="Insert" />  </insertitemtemplate>



- 我使用了表单视图供用户输入。我刚刚尝试实现一种投票类型的机制,当选择一个单选按钮并按下一个按钮时,就会存储一个cookie来对照该人在数据库中的条目。



背后的代码是:


- I have used a form view for the user to input. I have just tried to implement a voting sort of a mechanism in which when a radio button is selected and a button is pressed, then a cookie will be stored against that person's entry in the database.

The code behind is :

protected void Page_Load(object sender, EventArgs e)
      {
          if(Request.Cookies["Name"]==null)
          {
              string identifier = Guid.NewGuid().ToString();
              HttpCookie namecoockie = new HttpCookie("Name",identifier);
              namecoockie.Expires = DateTime.MaxValue;
              Response.AppendCookie(namecoockie);


          }
      }



- 我想做的是当用户选择一个单选按钮然后将对应于该单选按钮选择的cookie存储到数据库中。

- 我是成功的,但问题是我的cookie只在列中重复,我希望在Name和Contact列中输入cookie。

- 我怀疑cookie是string类型。我的数据库的Name列也是string类型,但是Contact列的类型是int。

- 这是什么原因?因为在insert参数中我已经清楚地指定了要插入的内容。





谢谢,


- What i wanted to do is that when ever user selects a radio button then a cookie corresponding to that radio button selection will be stored into the data base.
- I am successfull in it, but the catch is that my cookie is being duplicated in only column, where as i want the entry of the cookie in both Name and Contact column.
- My doubt is that the cookie is of type string. The "Name" column of my database is also of type string, but "Contact" column is of type int.
- Is this the reason? Because in the insert parameter i have clearly specified what to insert where.


Thanks,


这篇关于如何在数据库中存储cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆