日期:2014-05-17  浏览次数:20540 次

我是.net新手,请大家帮我看看这段程序吧,连不上数据库,但运行也不报错
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 _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  SqlConnection con = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
  con.Open();
  string username = this.TextBox2.Text.Trim();
   
  string strSql = "SELECT * FROM yonghu WHERE name = '" + username + "'";
  SqlCommand ob = new SqlCommand(strSql, con);
  SqlDataReader dt = ob.ExecuteReader();
  if (dt.Read())
  {

  Response.Write("<script>alert('用户名不可用!');</script>");
   
  }
  else
  {
  dt.Close();
  string a = this.T2.Value;
  string b = this.T3.Value;
  if (a != b)
  {
  Response.Write("<script>alert('两次密码输入不相同!');</script>");
  }
  else
  {
  string c = this.T4.Value;
   
  string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a + "')";
  ob.ExecuteNonQuery(); 
  Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
  Response.Redirect("mima.aspx");
  Response.Write("<script>history.go(-1);</script>");
   
  }
   
  }
  }
}


------解决方案--------------------
string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a + "')";
ob.ExecuteNonQuery(); 


最后这段怎么可能会执行呢,你只是写了sql语句 但是并没有执行它,
和上面一样的
sqlcommand comm=new sqlcommand(str,con);
int ret =comm.ExecuteNonQuery();
if(ret>0)


else


------解决方案--------------------
最后注意要关闭sql;con.close();
如果不想这样操作 你可以在用using()
------解决方案--------------------
楼主的结帖率好像不够高啊,


public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
con.Open();
string username = this.TextBox2.Text.Trim();
 
string strSql = "SELECT * FROM yonghu WHERE name = '" + username + "'";
SqlCommand ob = new SqlCommand(strSql, con);
SqlDataReader dt = ob.ExecuteReader();
if (dt.Read())
{

Response.Write("<script>alert('用户名不可用!');</script>");
con.Close();
 
}
else