<script language="C#" runat="server">
private void Page_Load(object sender,System.EventArgs e)
{
   String strConnection = "server = (local)\\FUBINLIN;database=Northwind;integrated security=true;";
   SqlConnection objConnection = new SqlConnection(strConnection);
   
   String strSQL = "SELECT FirstName,LastName,Country " + "FROM Employees";
   SqlCommand objCommand = new SqlCommand(strSQL,objConnection);
   
   objConnection.Open();
   dgNameList.DataSource = objCommand.ExecuteReader();
   dgNameList.DataBind();
   objConnection.Close();
   
}
</script>
<%@ Import namespace="System.Data.SqlClient"%>
<%@ Import namespace="System.Data"%>
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Beginning ASP.NET Databases Using C#</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<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>
<h4>First Example:Listing data from the Employee table</h4>
<asp:DataGrid ID="dgNameList" Runat="server" GridLines="None" BackColor="LightBlue" CellPadding="5"
CellSpacing="5" BorderWidth="2" BorderColor="Black" ToolTip="Include only those employees who are at HQ" />
</body>
</HTML>数据库我也打开了,运行时提示错误为:数据库不存在或数据库拒绝访问

解决方案 »

  1.   

    String strConnection = "server = (local);database=Northwind;integrated security=true;"; 
      

  2.   

    String strConnection = "server = (local)\\FUBINLIN;database=Northwind;integrated security=true;"; 建议将这句拆开来写,因为服务器名server = (local)\\FUBINLIN 。直接这么写可能会存在问题。
    stringBuffer str=new stringBuffer();
    str.Append(“server=“).Append(”(local)\\FUBINLIN“)
    。。