用.net写的注册程序,运行是好的,可就是提交信息时说数据库不存在或访问被拒绝,具体错误是SQL Server 不存在或访问被拒绝。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。
源错误:
行 72: SqlConnection Conn=new SqlConnection(connstr);
行 73: SqlCommand mycommand=new SqlCommand(strsql,Conn);
行 74: Conn.Open(); //错误
行 75: int flag=(int)mycommand.ExecuteScalar();
行 76: Conn.Close();
源程序是
private void BT_submit_Click(object sender, System.EventArgs e)
{
string connstr="server=localhost;database=aspnet;uid=sa;pwd=";
string strsql="select count(*)from UserInfo where UserID like'"+TB_UserName.Text+"'";
SqlConnection Conn=new SqlConnection(connstr);
SqlCommand mycommand=new SqlCommand(strsql,Conn);
Conn.Open();
int flag=(int)mycommand.ExecuteScalar();
Conn.Close();
if(flag==0)
{
string WebSite;
WebSite=Request.UserHostAddress.ToString();
string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website,RegTime)values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";
SqlCommand addcommand=new SqlCommand(selectstr,Conn);
Conn.Open(); ///出错
addcommand.ExecuteNonQuery ();
Conn.Close();
Session["username"]=TB_UserName.Text;
Session["Sex"]=RBL_Sex.SelectedItem.Value;
Label7.Text="注册成功,请先登录!";
}
else
{
Label7.Text="对不起,该用户名已经存在,请选择其他用户名!";
TB_UserName.Text="";
TB_Pwd.Text="";
TB_Pswd.Text="";
}
}
private void BT_reset_Click(object sender, System.EventArgs e)
{
Response.Redirect("Register.aspx");
}
}
}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。
源错误:
行 72: SqlConnection Conn=new SqlConnection(connstr);
行 73: SqlCommand mycommand=new SqlCommand(strsql,Conn);
行 74: Conn.Open(); //错误
行 75: int flag=(int)mycommand.ExecuteScalar();
行 76: Conn.Close();
源程序是
private void BT_submit_Click(object sender, System.EventArgs e)
{
string connstr="server=localhost;database=aspnet;uid=sa;pwd=";
string strsql="select count(*)from UserInfo where UserID like'"+TB_UserName.Text+"'";
SqlConnection Conn=new SqlConnection(connstr);
SqlCommand mycommand=new SqlCommand(strsql,Conn);
Conn.Open();
int flag=(int)mycommand.ExecuteScalar();
Conn.Close();
if(flag==0)
{
string WebSite;
WebSite=Request.UserHostAddress.ToString();
string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website,RegTime)values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";
SqlCommand addcommand=new SqlCommand(selectstr,Conn);
Conn.Open(); ///出错
addcommand.ExecuteNonQuery ();
Conn.Close();
Session["username"]=TB_UserName.Text;
Session["Sex"]=RBL_Sex.SelectedItem.Value;
Label7.Text="注册成功,请先登录!";
}
else
{
Label7.Text="对不起,该用户名已经存在,请选择其他用户名!";
TB_UserName.Text="";
TB_Pwd.Text="";
TB_Pswd.Text="";
}
}
private void BT_reset_Click(object sender, System.EventArgs e)
{
Response.Redirect("Register.aspx");
}
}
}
解决方案 »
- Girdview显示的数据 在下面加滚动条
- 有关于OA办公自动化可视化流程设计陷入了困境。。。。求解
- 请大家讨论一下webService
- 为什么不起作用?请大家帮我看看
- 像126邮箱重新设置信纸的功能如何实现?
- 版主,帮帮忙,有关URL重写的问题
- 最后20分求助:显示与隐藏GridView内容,用div可以?
- 如何获取对一个文件的排他访问权?
- SessionState 为 StateServer/SQLServer 如何触发 Session_OnEnd?
- 哪位高手大哥帮我看看我的网页视频代码为什么播放不出来视频
- CSDN不给分,只有10分了。。问4个问题,希望遇到好心人
- 我在浏览器里面地址栏填http://localhost/ , 看到的是 建设中
---------------------------
connstr="server=.;database=aspnet;uid=sa;pwd=";
还有,看看sql的设置,是否是混合登陆模式,这个要取决于你的连接串.
看看通不通
---------------------------
这句是关键!以前偶的就是设成了windows,搞半天连不上。
启动sql服务管理器,再打开企业管理器看看那个asp.net的数据库是否存在(保险起见)