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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/jianjianshini/article/details/106321393