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

sweetyfei 2012-05-15 01:15:50
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>");

}

}
}
}
...全文
161 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sweetyfei 2012-05-15
  • 打赏
  • 举报
回复
第一次来这个论坛,给各位的分数都打出来了,但是显示的怎么是0?如果真是这样,希望各位谅解,本人下次一定确定了再结贴~
shenyelive 2012-05-15
  • 打赏
  • 举报
回复
其实你这个 sqlcommand 这个还没有配置完
他的几个属性 cmd.CommandText = Text;这个属性得告诉sql你要执行什么是存储过程还是 sql文本
你这里是 文本Text
  • 打赏
  • 举报
回复
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>");


楼主 该结贴了 给分吧
sweetyfei 2012-05-15
  • 打赏
  • 举报
回复
哦。。。那怎么才能让那句话显示出来呢?
  • 打赏
  • 举报
回复
楼主的结帖率好像不够高啊,
Im_Sorry 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

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


不对。。是已经执行了。不过执行的是在当前页面
Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");……
[/Quote]

没看,听你解释的很有道理。+1
何年 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
嘿嘿,听取了大家的意见,和数据库连上了,但是Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
这句怎么不执行呢?
[/Quote]

不对。。是已经执行了。不过执行的是在当前页面
Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
然后你又跳转到 mima.aspx 页面了。。怎么可能看到。。
Response.Redirect("mima.aspx");
sweetyfei 2012-05-15
  • 打赏
  • 举报
回复
嘿嘿,听取了大家的意见,和数据库连上了,但是Response.Write("<script>alert('欢迎您成为本系统的项目申报人!');</script>");
这句怎么不执行呢?
sweetyfei 2012-05-15
  • 打赏
  • 举报
回复
嘿嘿,我刚刚注册的,很高兴大家来帮我,非常感谢啊,我做毕设呢,但是啥也不会,菜鸟一个,希望以后你们还能支持我~~
study_hard_01 2012-05-15
  • 打赏
  • 举报
回复
ckmin520 说的很对,还有一个小建议 就是楼主写的数据库相关操作可以单独放在一个类里面,你这样直接写,代码有点乱,可读性也不太强
  • 打赏
  • 举报
回复
楼主的结帖率好像不够高啊,


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();}
}

}
}
}

这代码写的真是乱遭了,哎,,,,
sweetyfei 2012-05-15
  • 打赏
  • 举报
回复
哦~~那别的地方还有错吗?我想让你帮我把这段程序都改一下~我按你说的改了,但还是不行,应该是我没明白你的意思吧,嘿嘿,麻烦啦~~再写的详细点~~
刹那的菜鸟 2012-05-15
  • 打赏
  • 举报
回复
楼上的说的没错!过来看看
  • 打赏
  • 举报
回复
最后注意要关闭sql;con.close();
如果不想这样操作 你可以在用using()
  • 打赏
  • 举报
回复
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

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧