我使用的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
}
}
}
}
谢谢
尝试使用'Debug.WriteLine'而不是'Console.WriteLine',没有连接到Web应用程序的控制台。 – 2012-03-07 19:10:26
不,它位于'System.Diagnostics'名称空间中,请按Ctrl +。 (Ctrl和Dot-key),然后使用弹出菜单添加所需的使用指令。 – 2012-03-07 19:18:14