using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class SystemManage_AddUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (Convert.ToString(Session["username"]) == "")
		{
			Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
		}
    }
	protected void Button1_Click(object sender, EventArgs e)
	{
		string username = Request["username"];
		string userpwd = Request["userpwd"];
		DateTime AddTime = DateTime.Now;
		if (Tools.validate(username))
		{
			Response.Write("<script>alert('用户名中不能含有非法字符');history.back()</script>");
			return;
		}
		if (Tools.validate(userpwd))
		{
			Response.Write("<script>alert('密码中不能含有非法字符');histort.back()</script>");
		}
		string sqlcmd1 = "select count(*) as ff from System_Users where username='" + username + "'";
		using(SqlConnection sqlconn = new SqlConnection(Tools.strConn))
		{
			sqlconn.Open();
			SqlCommand cmd = new SqlCommand(sqlcmd1, sqlconn);
			int count = Convert.ToInt32(cmd.ExecuteScalar());
			if(count > 0)
			{
				Response.Write("<script>alert('用户已经存在，请重新输入！');histort.back()</script>");
				return;
			}
			try
			{
				string sqlcmd2 = "insert into System_Users (username,userpwd,addtime)values(@username,@userpwd,@addtime)";
				cmd.CommandText = sqlcmd2;
				cmd.Parameters.Add("@username", SqlDbType.VarChar, 20).Value = username;
				cmd.Parameters.Add("@userpwd", SqlDbType.VarChar, 20).Value = userpwd;
				cmd.Parameters.Add("@addtime", SqlDbType.DateTime).Value = AddTime;
				cmd.ExecuteNonQuery();
                Response.Write("<script>alert('用户添加成功');location='SystemUserManage.aspx'</script>");
			}
			catch
			{
				Response.Write("<script>alert('操作失败');location='AddUser.aspx'</script>");
			}
		}
	}
}
