最近在学ASP.NET,可是在跟SQL连接老是出现这个错误是怎么回事啊..急啊...
我已经打开SQL SERVER了..下面是错误的提示信息:
望高手们教教小弟吧....谢了...
SQL Server 不存在或访问被拒绝。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。
这是我在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编辑过]
|
|