| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 790 人关注过本帖
标题:[求助]ASP.NET跟SQL连接出错..
只看楼主 加入收藏
hap163
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2005-11-9
收藏
 问题点数:0 回复次数:7 
[求助]ASP.NET跟SQL连接出错..

最近在学ASP.NET,可是在跟SQL连接老是出现这个错误是怎么回事啊..急啊...
我已经打开SQL SERVER了..下面是错误的提示信息:
望高手们教教小弟吧....谢了...


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

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


搜索更多相关主题的帖子: SQL NET ASP 
2006-03-17 15:20
hap163
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2005-11-9
收藏
得分:0 

这是我在web.config中定义的连接...
<appSettings>
<add key="DSN" value = "server=(local);database=myDatabase;UID=sa;pwd=123456"></add>
</appSettings>

这是前台的代码:

<%@ Page language="c#" Codebehind="011SimpleBind.aspx.cs" AutoEventWireup="false" Inherits="ADOCOMMAND.SimpleBind" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SimpleBind</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="SimpleBind" method="post" runat="server">
<asp:TextBox id=tbID style="Z-INDEX: 100; LEFT: 331px; POSITION: absolute; TOP: 64px" runat="server" Text='<%# DataBinder.Eval(dsStudent1, "Tables[tbStudentinfo].DefaultView.[0].StudentID") %>'>
</asp:TextBox>
<asp:Button id="btnNext" style="Z-INDEX: 111; LEFT: 405px; POSITION: absolute; TOP: 258px" runat="server"
Text="下一条"></asp:Button>
<asp:Label id="Label6" style="Z-INDEX: 108; LEFT: 199px; POSITION: absolute; TOP: 166px" runat="server"
Width="83px">分数</asp:Label>
<asp:Label id="Label5" style="Z-INDEX: 107; LEFT: 199px; POSITION: absolute; TOP: 112px" runat="server"
Width="83px">名字</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 101; LEFT: 199px; POSITION: absolute; TOP: 64px" runat="server"
Width="83px">ID</asp:Label>
<asp:TextBox id=tbScore style="Z-INDEX: 106; LEFT: 334px; POSITION: absolute; TOP: 155px" runat="server" Text='<%# DataBinder.Eval(dsStudent1, "Tables[tbStudentinfo].DefaultView.[0].Score") %>'>
</asp:TextBox>
<asp:Label id="Label3" style="Z-INDEX: 102; LEFT: 199px; POSITION: absolute; TOP: 64px" runat="server"
Width="83px">ID</asp:Label>
<asp:TextBox id=tbName style="Z-INDEX: 105; LEFT: 332px; POSITION: absolute; TOP: 105px" runat="server" Text='<%# DataBinder.Eval(dsStudent1, "Tables[tbStudentinfo].DefaultView.[0].StudentName") %>'>
</asp:TextBox>
<asp:Label id="Label2" style="Z-INDEX: 103; LEFT: 199px; POSITION: absolute; TOP: 64px" runat="server"
Width="83px">ID</asp:Label>
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 199px; POSITION: absolute; TOP: 65px" runat="server"
Width="83px">ID</asp:Label>
<asp:Button id="btnPrev" style="Z-INDEX: 110; LEFT: 224px; POSITION: absolute; TOP: 258px" runat="server"
Text="上一条"></asp:Button>
</form>
</body>
</HTML>


这是后台的:
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;

namespace ADOCOMMAND
{
/// <summary>
/// SimpleBind 的摘要说明。
/// </summary>
public class SimpleBind : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected ADOCOMMAND.DSStudent dsStudent1;
protected System.Web.UI.WebControls.TextBox tbID;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox tbName;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox tbScore;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button btnPrev;
protected System.Web.UI.WebControls.Button btnNext;
protected System.Web.UI.WebControls.Label Label6;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{

Session["RecordPos"] = 0;


}
sqlDataAdapter1.Fill(dsStudent1.tbStudentinfo);
this.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dsStudent1 = new ADOCOMMAND.DSStudent();
((System.ComponentModel.ISupportInitialize)(this.dsStudent1)).BeginInit();
this.tbID.DataBinding += new System.EventHandler(this.tbID_DataBinding);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.tbScore.TextChanged += new System.EventHandler(this.tbScore_TextChanged);
this.tbScore.DataBinding += new System.EventHandler(this.tbScore_DataBinding);
this.tbName.DataBinding += new System.EventHandler(this.tbName_DataBinding);
this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT StudentID, StudentName, StudentPass, Sex, Birthday, Email, Score FROM tbSt" +
"udentinfo";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=SHAOZHD;initial catalog=myDatabase;integrated security=SSPI;persist s" +
"ecurity info=True;workstation id=SHAOZHD;packet size=4096";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO tbStudentinfo(StudentName, StudentPass, Sex, Birthday, Email, Score) " +
"VALUES (@StudentName, @StudentPass, @Sex, @Birthday, @Email, @Score); SELECT Stu" +
"dentID, StudentName, StudentPass, Sex, Birthday, Email, Score FROM tbStudentinfo" +
"";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@StudentName", System.Data.SqlDbType.VarChar, 50, "StudentName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@StudentPass", System.Data.SqlDbType.VarChar, 50, "StudentPass"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Sex", System.Data.SqlDbType.Bit, 1, "Sex"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Birthday", System.Data.SqlDbType.DateTime, 8, "Birthday"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Email", System.Data.SqlDbType.VarChar, 50, "Email"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Score", System.Data.SqlDbType.Int, 4, "Score"));
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "tbStudentinfo", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("StudentID", "StudentID"),
new System.Data.Common.DataColumnMapping("StudentName", "StudentName"),
new System.Data.Common.DataColumnMapping("StudentPass", "StudentPass"),
new System.Data.Common.DataColumnMapping("Sex", "Sex"),
new System.Data.Common.DataColumnMapping("Birthday", "Birthday"),
new System.Data.Common.DataColumnMapping("Email", "Email"),
new System.Data.Common.DataColumnMapping("Score", "Score")})});
//
// dsStudent1
//
this.dsStudent1.DataSetName = "DSStudent";
this.dsStudent1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dsStudent1)).EndInit();

}
#endregion

private void btnPrev_Click(object sender, System.EventArgs e)
{

int RecordPos = (int) Session["RecordPos"];
if (RecordPos > 0)
{
RecordPos--;
}
Session["RecordPos"] = RecordPos;
this.DataBind();
}

private void btnNext_Click(object sender, System.EventArgs e)
{
int RecordPos = (int) Session["RecordPos"];
RecordPos++;
if (RecordPos >= dsStudent1.tbStudentinfo.Rows.Count)
{
RecordPos=0;
}
Session["RecordPos"] = RecordPos;
this.DataBind();
}

private void tbID_DataBinding(object sender, System.EventArgs e)
{
int RecordPos = (int) Session["RecordPos"];
tbID.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["StudentID"].ToString();
}

private void tbName_DataBinding(object sender, System.EventArgs e)
{
int RecordPos = (int) Session["RecordPos"];
tbName.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["StudentName"].ToString();

}

private void tbScore_DataBinding(object sender, System.EventArgs e)
{
int RecordPos = (int) Session["RecordPos"];
tbScore.Text = dsStudent1.tbStudentinfo.Rows[RecordPos]["Score"].ToString();

}

private void tbScore_TextChanged(object sender, System.EventArgs e)
{

}
}
}

[此贴子已经被作者于2006-3-17 15:57:47编辑过]

2006-03-17 15:54
hap163
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2005-11-9
收藏
得分:0 
我一运行就发生这种错误:

SQL Server 不存在或访问被拒绝。

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

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

源错误:

行 43: 				
行 44: 			}
行 45: 			sqlDataAdapter1.Fill(dsStudent1.tbStudentinfo);
行 46: 			this.DataBind();
行 47: 		}

源文件: e:\net\asp.net\adocommand\011simplebind.aspx.cs 行: 45

堆栈跟踪:

[SqlException: SQL Server 不存在或访问被拒绝。]
   System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) +472
   System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) +372
   System.Data.SqlClient.SqlConnection.Open() +384
   System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState) +44
   System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable, IDbCommand command, CommandBehavior behavior) +28
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +88
   ADOCOMMAND.SimpleBind.Page_Load(Object sender, EventArgs e) in e:\net\asp.net\adocommand\011simplebind.aspx.cs:45
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +731
2006-03-17 15:56
nothing
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2005-11-10
收藏
得分:0 
这样肯定有问题呀!
using System.Data.SqlClient;
这个命名空间都没有

2006-03-17 16:13
hap163
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2005-11-9
收藏
得分:0 
我现在加上去了..还不行..
2006-03-17 16:30
tz_xiong
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-2
收藏
得分:0 

检查你的连接字符串,然后看你的SQL是采用什么验证方式,可以做一个Command简单查询字符串来测试一下的.

2006-03-18 01:31
water123456
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:3208
专家分:5
注 册:2006-2-21
收藏
得分:0 
怎么样来做呀?

让我们风风火火的过一生,别平平淡淡过一年。
2006-03-18 07:29
hap163
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2005-11-9
收藏
得分:0 

可以用了..谢谢各位....
但不懂是为什么.....
我把:server=(local);换成server=coolzgt就可以了...
coolzgt为我SQL服务的名称....
请为大虾们这是为什么....

2006-03-18 15:42
快速回复:[求助]ASP.NET跟SQL连接出错..
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031133 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved