![](https://img-my.csdn.net/uploads/201205/12/1336817887_7839.jpg)
using System; using System.Data; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; namespace WestGarden.Web { public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Page.Header.Title = "肯德基订餐系统[西园工作室]::用户注册"; } protected void btnRegist_Click(object sender, EventArgs e) { if (Page.IsValid) { string strConnection = ConfigurationManager.ConnectionStrings["NetShopConnString"].ConnectionString; string cmdText = "INSERT INTO Users(Username,Password,Name,Sex,Age,Phone,Email,QQ) values(@Username,@Password,@Name,@Sex,@Age,@Phone,@Email,@QQ)"; string username = txtUserName.Text; string password = txtPassword.Text; string name = txtName.Text; string sex = radlSex.SelectedValue; int age = int.Parse(txtAge.Text); string phone = txtPhone.Text; string email = txtEmail.Text; string qq = txtQQ.Text; SqlParameter[] parms ={ new SqlParameter("@UserName", SqlDbType.VarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 20), new SqlParameter("@Name", SqlDbType.VarChar, 20), new SqlParameter("@Sex", SqlDbType.VarChar, 2), new SqlParameter("@Age", SqlDbType.Int), new SqlParameter("@Phone", SqlDbType.VarChar, 20), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@QQ", SqlDbType.VarChar, 20)}; parms[0].Value = username; parms[1].Value = password; parms[2].Value = name; parms[3].Value = sex; parms[4].Value = age; parms[5].Value = phone; parms[6].Value = email; parms[7].Value = qq; SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(strConnection); try { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = cmdText; foreach (SqlParameter parm in parms) cmd.Parameters.Add(parm); cmd.ExecuteNonQuery(); } catch { Response.Redirect("ErrorMessage.aspx"); } finally { conn.Close(); } Response.Redirect("Login.aspx"); } } } }