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 AdoNetObject
{
/// <summary>
/// Summary description for Connection.
/// </summary>
public partial class Connection : System.Web.UI.Page
{
private String ConnectionString = "data source=dengjifu;Database=jwgl;uid=fu;pwd=5591960;";
protected void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
ConnectionObject();
}
}
private void ConnectionObject()
{
SqlConnection sqlConnection = new SqlConnection(ConnectionString);
sqlConnection.Open();
Response.Write("<script>alert('" + sqlConnection.ConnectionString.Replace(";", "\\n") + "\\n数据库的连接成功!!!');</script>");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: TCP 提供程序, error: 0 - 由于目标机器积极拒绝,无法连接。)