当前位置: 代码迷 >> ASP.NET >> asp.net连接数据库的有关问题
  详细解决方案

asp.net连接数据库的有关问题

热度:2750   发布时间:2013-02-25 00:00:00.0
asp.net连接数据库的问题
小弟正在学习asp.net
连接数据库连不上
代码如下(visual   studio平台):
using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;
using   System.Data.SqlClient   ;
namespace   studyaspnet.connectsqlsever
{
///   <summary>
///   WebForm1   的摘要说明。
///   </summary>
public   class   WebForm1   :   System.Web.UI.Page
{
private   void   Page_Load(object   sender,   System.EventArgs   e)
{
string   con= "Server=localhost;Integrated   Security=sspi;Initial   Catalog=mywebdb ";//   在此处放置用户代码以初始化页面
SqlConnection   myconnection=new   SqlConnection   (con);
myconnection.Open();
Response.Write   ( "OK------------ ");
myconnection.Close   ();
}
在浏览器中查看显示这样的错误信息:
“/studyaspnet”应用程序中的服务器错误。
--------------------------------------------------------------------------------

SQL   Server   不存在或访问被拒绝。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.Data.SqlClient.SqlException:   SQL   Server   不存在或访问被拒绝。

源错误:  


行   21:   string   con= "Server=localhost;Integrated   Security=SSPI;Initial   Catalog=mywebdb ";//   在此处放置用户代码以初始化页面
行   22:   SqlConnection   myconnection=new   SqlConnection   (con);
行   23:   myconnection.Open();
行   24:   Response.Write   ( "OK------------ ");
行   25:   myconnection.Close   ();
 

源文件:   c:\inetpub\wwwroot\studyaspnet\connectsqlsever\webform1.aspx.cs         行:   23  

堆栈跟踪:  


[SqlException:   SQL   Server   不存在或访问被拒绝。]
      System.Data.SqlClient.SqlConnection.Open()
      studyaspnet.connectsqlsever.WebForm1.Page_Load(Object   sender,   EventArgs   e)   in   c:\inetpub\wwwroot\studyaspnet\connectsqlsever\webform1.aspx.cs:23
      System.Web.UI.Control.OnLoad(EventArgs   e)
      System.Web.UI.Control.LoadRecursive()
      System.Web.UI.Page.ProcessRequestMain()

 


--------------------------------------------------------------------------------
版本信息:   Microsoft   .NET   框架版本:1.0.3705.0;   ASP.NET   版本:1.0.3705.0  


谢谢各位各位大虾指点!


------解决方案--------------------------------------------------------
1. 你用的貌似Visual Studio 2003, 最要不要用集成身份验证的方式(需要一些其他的设置才可以),而是使用
string con = "Server=(local);uid=sa;pwd=你的密码;database=mywebdb ";

2.看你现在出错的信息,最好先检查你的SQL Server服务是否已经启动
------解决方案--------------------------------------------------------
string strFile=null;
try
{
String strPath=System.Web.HttpContext.Current.Server.MapPath( " ");
StreamReader sr = new StreamReader(strPath+ "\\Set.txt ");//读取虚拟目录下的文本文件中的内容
strFile = sr.ReadLine(); //读取
sr.Close();//关闭读取
}
catch
{

}

Sqlcon=new SqlConnection(strFile);
  相关解决方案