ASP.NET更新数据库中的信息

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>在线选人系统</h2>
        <asp:RadioButtonList ID="radBtnList" runat="server" ></asp:RadioButtonList>
        <asp:Button ID="btn" runat="server" Text="投票" onclick="btn_Click"/>
    </div>
    </form>
</body>
</html>

数据库连接类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;


namespace WebApplication1
{
    public class DB
    {
        public static SqlConnection sqlcon()
        {
            return new SqlConnection("server=.;database=vote;uid=sa;pwd=789456");
        }
    }
}

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //在网页第一次打开的时候进行调用
            if (!IsPostBack) {
                this.dataBind();
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            string voteId = "";
            string voteName = "";
            int num = 0;
            for (int i = 0; i < this.radBtnList.Items.Count; i++)
            {
                if (this.radBtnList.Items[i].Selected) {
                    voteId = this.radBtnList.Items[i].Value;
                    voteName = this.radBtnList.Items[i].Text;
                    SqlConnection sqlcon = DB.sqlcon();
                    sqlcon.Open();
                    SqlCommand sqlcom = new SqlCommand("update voteTab set voteNum=voteNum+1 where voteId=' " + voteId + " ' ", sqlcon);
                    sqlcom.ExecuteNonQuery();   //返回的是受影响的行数
                    sqlcom.CommandText="select voteNum from voteTab where voteId=' " +voteId+" ' ";
                    num = Convert.ToInt32(sqlcom.ExecuteScalar());
                    Response.Write(voteName+"获得的票数:"+num);
                }
            }
            if (num == 0) {
                //当什么都没选中就提交的时候,进行提示
                Response.Write("<script language='javascript'>alert('请选择投票的对象!');</script>");
            }
        }

        /// <summary>
        /// 数据绑定方法
        /// </summary>
        private void dataBind()
        {
            SqlConnection sqlcon = DB.sqlcon();
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand("select * from voteTab", sqlcon);
            SqlDataReader dr = sqlcom.ExecuteReader();
            this.radBtnList.DataSource = dr;
            this.radBtnList.DataValueField = "voteId";
            this.radBtnList.DataTextField = "voteName";
            this.radBtnList.DataBind();
            sqlcon.Close();
        }
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Defining the Future

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值