judge.ashx

来自「这是一个很好的图书管理系统 数据库SQL2005」· ASHX 代码 · 共 67 行

ASHX
67
字号
<%@ WebHandler Language="C#" Class="judge" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class judge : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string id = context.Request.QueryString["id"].ToString();
        bool ret = JudgeUserID(id);
        if (ret)
        {
            context.Response.Write(id+"可以使用");
        }
        else
        {
            context.Response.Write(id+"已存在");
        }
    }

    private bool JudgeUserID(string id)
    {
        int row = 0;
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.AppSettings["strconn"].ToString();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "select count(*) from T_User where username=@username";
            comm.Parameters.Add("@username", SqlDbType.NVarChar, 20).Value = id;
            try
            {
                conn.Open();
                row = int.Parse(comm.ExecuteScalar().ToString());
                conn.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("检索数据库出错", ex);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }

        }
        if (row > 0)
        {
            return false;
        }
        else
            return true;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?