2012-03-07 66 views
2

我使用的Visual Web Developer 2010 Express和SQL Server 2008 R2的管理Studio Express的为什么不会C#显示表

嗨,

在C#在这里很新。我试图按照this C#ADO.NET教程(目前在步骤2),我很难过。我遵循所有步骤,并且对其中的所有内容都有意义,但每当我尝试调试时,它都不会显示任何内容(从c#方法的意义上说,不会将Northwind数据库中的表格打印到我的网页上) WebApplication1的Default.aspx页面。

有一段时间,我认为这是我的连接字符串conn,我没有命名"Data Source"属性,据我了解这是我试图连接的服务器的名称。它全部在本地机器上,并且我正在输入正确的服务器名称。我想。服务器名称是AZUES-221\JDOESQLSERVER

我正确地逃避反向斜线,但我仍然不知道。我的编码有什么缺陷吗?请帮忙!

C#代码提前

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Services; 

namespace WebApplication1 
{ 
    public partial class SqlConnectionDemo : System.Web.UI.Page 
    { 


     protected void Main(object sender, EventArgs e) 
     { 
      SqlConnection conn = new SqlConnection("Data Source=AZUES-221\\JDOESQLSERVER; Initial Catalog=Northwind; Integrated Security=SSPI"); 

      SqlDataReader rdr = null; 

      try 
      { 
       conn.Open(); 
       SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); //passed the connection 

       rdr = cmd.ExecuteReader(); // get query results 

       while (rdr.Read()) //prints out whatever was 
       { Console.WriteLine(rdr[0]); }//selected in the table 
      } 

      finally 
      { 
       if (rdr != null)// closes 
       { rdr.Close(); }// the reader 

       if (conn != null)//closes 
       { conn.Close(); }// the connection 

      } 
     } 
    } 
} 

谢谢

+6

尝试使用'Debug.WriteLine'而不是'Console.WriteLine',没有连接到Web应用程序的控制台。 – 2012-03-07 19:10:26

+1

不,它位于'System.Diagnostics'名称空间中,请按Ctrl +。 (Ctrl和Dot-key),然后使用弹出菜单添加所需的使用指令。 – 2012-03-07 19:18:14

回答

3

当你的例子似乎是一个WebProject尽量把Page_Load事件处理程序中的代码。之后,您应该尝试将数据打印到Debug窗口或网页中的控件。

using System; 
using System.Data; 
// and all the others ... 

namespace WebApplication1 
{ 
    public partial class SqlConnectionDemo : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SqlConnection conn = new SqlConnection("Data Source=AZUES-221\\JDOESQLSERVER; Initial Catalog=Northwind; Integrated Security=SSPI"); 
     SqlDataReader rdr = null; 

     try 
     { 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); 
      rdr = cmd.ExecuteReader(); // get query results 

      while (rdr.Read()) //prints out whatever was 
      { 
       System.Diagnostics.Debug.WriteLine(rdr[0]); // or on the other hand 
       lblOutput.Text += rdr[0]; // as a "quick and dirty" solution! 
      } 
     } 

     finally 
     { 
      if (rdr != null)// closes 
      { rdr.Close(); }// the reader 
      if (conn != null)//closes 
      { conn.Close(); }// the connection 
     } 
    } 
    } 
} 

您可能会发现它非常有用的,看看databound controls或者只是使用另一种类型的项目(如WinForm的,控制台,...)

0

为什么会console.writeline显示任何东西。你没有在控制台上工作。

万一只看到你的输出。使用Response.writeline(rdr [0]);

1

创建一个控制台应用程序而不是您创建的Web应用程序。 否则,考虑到您对C#(或一般Visual Studio)不熟悉,并且考虑到本教程的其余部分大量使用Console.WriteLine,您会遇到类似的问题。

然后,您可以使用教程中显示的相同代码。

Console Application from Visual Studio 2008

Additonally如果您担心在数据库服务器中的斜杠(它是一个数据库服务器实例),你可能会想试试这个:

SqlConnection conn = new SqlConnection(@"Server=AZUES-221\JDOESQLSERVER;Database=Northwind;Trusted_Connection=True;"); 

来源:Connection Strings Reference