小弟正在学习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 谢谢各位各位大虾指点!
连接数据库连不上
代码如下(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 谢谢各位各位大虾指点!
改下string con="Server=localhost;UserID=sa;Password=123;database=mywebdb;Connection Reset=FALSE">数据库登陆方式改成混合验证,用户名sa,密码123
回3楼:我按你说的改了,还是不行,问题应该不在这我觉得,信任连接连不了,非信任连接应该也连不了
谢谢二位的回答!
string con = "Server=(local);uid=sa;pwd=你的密码;database=mywebdb";2.看你现在出错的信息,最好先检查你的SQL Server服务是否已经启动
应该还是连接串的问题;
你可以在查询分析器里,用用户名和密码尝试下连接,确认用户名和密码正确;
这是学习.NET的最好方式
但我还想问一下:
1,我是vs2003,如果希望实现信任连接应该怎么设置?
2,Server=(local);是正确的,换成server=localhost,就不行了,为什么?书上就是用的sever=localhost
谢谢大家的回答,明天结帖!
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);
Sqlcom=new SqlCommand();
Sqlcom.Connection=Sqlcon;
Sqlds=new DataSet();
Sqlda=new SqlDataAdapter();
Set.txt文件里是这样写的:
data source=.;database=inkosi;uid=用户名;password=密码