表单内容提交到数据库案例

时间:2022-10-13 16:39:45

配置好Web.config,在configuration里面添加节点connectionStrings:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <connectionStrings>
      <add name="connstr" connectionString="server=AAA;user id=sa;password=123;database=lesson"/>
    </connectionStrings>
</configuration>

写好MySqlHelper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace MySqlHelper
{
    public class SQLHelper
    {
        private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns></returns>
        public static SqlConnection CreateConnection()
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            return conn;
        }
        /// <summary>
        /// 返回受影响行数
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 按现有连接,返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = CreateConnection())
            {
                return ExecuteNonQuery(conn,sql,parameters);
            }
        }
        /// <summary>
        /// 返回第一行第一条数据
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteScalar();
            }
        }
        /// <summary>
        /// 按现有连接,返回第一行第一条数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = CreateConnection())
            {
                return ExecuteScalar(conn,sql,parameters);
            }
        }
        /// <summary>
        /// 返回多条数据
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataTable ExecuteReader(SqlConnection conn, string sql, params SqlParameter[] parameters)
        {
            DataTable table = new DataTable();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parameters);
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    table.Load(reader);
                }
            }
            return table;
        }
        /// <summary>
        /// 按现有连接,返回多条数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataTable ExecuteReader(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = CreateConnection())
            {
                return ExecuteReader(conn,sql,parameters);
            }
        }

    }
}

 

html代码:

<form action="Text1.ashx" method="get">
    姓名:<input type="text" name="username" value="" />
    </p>
    年龄:<input type="text" name="age" value="" />
    </p>
    <input type="checkbox" name="ch1" value="true" />是否富二代
    </p>
    专业:<select name="se1">
        <option value="计算机">计算机</option>
        <option value="管理学">管理学</option>
        <option value="化学">化学</option>
        <option value="机械">机械</option>
    </select>
    </p>
    性取向:<input type="radio" name="ra1" value="喜欢男" />喜欢男
            <input type="radio" name="ra1" value="喜欢女的" />喜欢女的
            <input type="radio" name="ra1" value="通吃" />通吃
    </p>
    <input type="submit" name="btn" value="保存" />
</form>

一般处理程序代码:

context.Response.ContentType = "text/html";
string username = context.Request["username"];
int age = Convert.ToInt32(context.Request["age"]);
bool gener = Convert.ToBoolean(context.Request["ch1"]);
string prof = context.Request["se1"];
string sex = context.Request["ra1"];
int r = SQLHelper.ExecuteNonQuery("insert into Users(username,age,gener,prof,sex) values(@username,@age,@gener,@prof,@sex)",
    new SqlParameter("@username",username),
    new SqlParameter("@age", age),
    new SqlParameter("@gener", gener),
    new SqlParameter("@prof", prof),
    new SqlParameter("@sex", sex));
if (r > 0)
{
    context.Response.Write("插入成功");
}
else
{
    context.Response.Write("插入失败");
}