问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Data.OleDb;usingSystem.Data;namespaceIndexEvaluationSystem.Master{publicpartialclassWebForm2:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton_Regist_Click(objectsender,EventArgse){if(this.IsValid){SqlConnectioncn=newSqlConnection(ConfigurationManager.ConnectionStrings["IndexEvalutionSystemConnectionString"].ConnectionString);SqlCommandcmd=newSqlCommand("INSERTUSER(username,password,group,phone,email,userauthority)VALUES(@username,@password,@group,@phone,@email,@userauthority)",cn);cmd.Parameters.Add("@username",SqlDbType.NVarChar).Value=TextBox_UserName.Text;cmd.Parameters.Add("@password",SqlDbType.NVarChar).Value=TextBox_Password_Second.Text;cmd.Parameters.Add("@group",SqlDbType.NVarChar).Value=DropDownList1.Text;cmd.Parameters.Add("@phone",SqlDbType.NVarChar).Value=TextBox_Phone.Text;cmd.Parameters.Add("@email",SqlDbType.NVarChar).Value=TextBox_Email.Text;cmd.Parameters.Add("@userauthority",SqlDbType.NVarChar).Value="低";try{cn.Open();cmd.ExecuteNonQuery();ClientScript.RegisterStartupScript(this.GetType(),"Key","<script>alert('注册成功!');</script>");}catch{ClientScript.RegisterStartupScript(this.GetType(),"Key","<script>alert('注册失败!');</script>");}finally{cn.Close();}}}}}
解决方案
解决方案二:
SqlCommandcmd=newSqlCommand("INSERTUSER([username],[password],[group],[phone],[email],[userauthority])VALUES(@username,@password,@group,@phone,@email,@userauthority)",cn);
oledb需要在字段外面添加[]符号我也之前也是这样写出错了~你测试下~应该没问题
解决方案三:
引用1楼iislove的回复:
SqlCommandcmd=newSqlCommand("INSERTUSER([username],[password],[group],[phone],[email],[userauthority])VALUES(@username,@password,@group,@phone,@email,@userauthority)",cn);oledb需要在字段外面添加[]符号我也之前也是这样写出错了~你测试下~应该没问题
而且你代码好像也是写错了我复制了你没看到~不能编辑应该是INSERTINTOUSER
INSERTUSER这不能插入表格
解决方案四:
引用2楼iislove的回复:
Quote: 引用1楼iislove的回复:
SqlCommandcmd=newSqlCommand("INSERTUSER([username],[password],[group],[phone],[email],[userauthority])VALUES(@username,@password,@group,@phone,@email,@userauthority)",cn);oledb需要在字段外面添加[]符号我也之前也是这样写出错了~你测试下~应该没问题
而且你代码好像也是写错了我复制了你没看到~不能编辑应该是INSERTINTOUSER
INSERTUSER这不能插入表格
谢谢
解决方案五:
catch{ClientScript.RegisterStartupScript(this.GetType(),"Key","<script>alert('注册失败!');</script>");}finallycatch的时候不要把异常吃掉,异常信息很多情况下可以帮忙定位错误,如果你不想显示错误信息给用户,至少应该写下log。
解决方案六:
把异常信息显示出来,就知道具体是什么错误了嘛
解决方案七:
VALUES(@username,@password,@group,@phone,@email,@userauthority)varcharnvarchar类型要加单引号
解决方案八:
引用6楼Foreign_Key的回复:
VALUES(@username,@password,@group,@phone,@email,@userauthority)varcharnvarchar类型要加单引号
这样并不需要