日期:2014-05-18  浏览次数:20933 次

菜鸟 求教
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Scripts_Default : System.Web.UI.Page
{
  protected void Button3_Click(object sender, EventArgs e)
  {
  string Name = TextBox1.Text.Trim();
  string Password = TextBox2.Text.Trim();
   
  string Password1 = TextBox3.Text.Trim();
   
  String strCon = "Data Source=127.0.0.1;Integrated Security=SSPI ; Initial Catalog=user";
  SqlConnection conn = new SqlConnection(strCon);

   
  conn.Open();
   
  string sql = "insert into [yl] values('" + 3 + "','" + Name + "','"+Password+"')";
  SqlCommand cmd = new SqlCommand(sql, conn);

  int i = cmd.ExecuteNonQuery();
  if (i> 0)
  {
  Response.Write("<script>alert('注册成功');</script>");
  }
  else
  {
  Response.Write("<script>alert('注册不成功,请检查输入信息');</script>");
  }
  conn.Close();
   
  }
}


我这个注册页面为什么 不能使数据库添加数据呢

------解决方案--------------------
string sql = "insert into [yl] values('" + 3 + "','" + Name + "','"+Password+"')";



string sql = "insert into [yl] values('3','" + Name + "','"+Password+"')";
------解决方案--------------------
你这个没有查到问题的根本所在啊
探讨

string sql = "insert into [yl] values('" + 3 + "','" + Name + "','"+Password+"')";



string sql = "insert into [yl] values('3','" + Name + "','"+Password+"')";

------解决方案--------------------
断点跟一下 看看到哪有异常
------解决方案--------------------
你看看你values后面跟的参数跟表里的列个数是否一致
------解决方案--------------------
检查表及字段类型是否有错
------解决方案--------------------
查看报错信息;

取出sql语句在数据库里执行看看。
------解决方案--------------------
还有,你alert提示的时候,不建议用
Response.Write("<script>alert('注册成功');</script>");
用:
 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('成功!')</script>");
------解决方案--------------------
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('成功!')</script>", false);