首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

小弟我是.net新手,请大家帮小弟我看看这段程序吧,连不上数据库,但运行也不报错

2012-05-16 
我是.net新手,请大家帮我看看这段程序吧,连不上数据库,但运行也不报错using Systemusing System.Collecti

我是.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


{
dt.Close();
con.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;
SqlConnection con1 = new SqlConnection("server=localhost;database=xitong;uid=sa;pwd=123");
con1.Open();
string str = "insert into yonghu (name,shenfenzhenghao,password)values('" + TextBox2.Text + "','" +c + "','" + a + "')";
SqlCommand comm= new SqlCommand(str, con1);
int ret= comm.ExecuteNonQuery(); 
con1.Close();
if(ret>0){
Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
Response.Redirect("mima.aspx");
Response.Write("<script>history.go(-1);</script>");
}else{con1.Close();}
}
 
}
}
}

这代码写的真是乱遭了,哎,,,,
[解决办法]
ckmin520 说的很对,还有一个小建议 就是楼主写的数据库相关操作可以单独放在一个类里面,你这样直接写,代码有点乱,可读性也不太强
[解决办法]

探讨
嘿嘿,听取了大家的意见,和数据库连上了,但是Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
这句怎么不执行呢?

[解决办法]
if(ret>0){
Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
Response.Redirect("mima.aspx");
Response.Write("<script>history.go(-1);</script>");
}else{con1.Close();}
}

把里面的换成 Response.Write("<script language='javascript'>alert('欢迎您成为本系统的项目申报人');winodw.location.href='mima.aspx'</script>");


楼主 该结贴了 给分吧
[解决办法]
其实你这个 sqlcommand 这个还没有配置完
他的几个属性 cmd.CommandText = Text;这个属性得告诉sql你要执行什么是存储过程还是 sql文本
你这里是 文本Text

热点排行