为什么赢了';t C#出示表格
我正在使用Visual Web Developer 2010 Express和SQL Server 2008 R2 Management Studio Express 嘿,伙计们 在这里的C#相当新。我正试图学习C#ADO.NET教程(目前在step上),我被难住了。我正在遵循所有步骤,其中的所有内容对我来说都是有意义的,但每当我尝试调试时,为什么赢了';t C#出示表格,c#,asp.net,sqlconnection,sqlcommand,C#,Asp.net,Sqlconnection,Sqlcommand,我正在使用Visual Web Developer 2010 Express和SQL Server 2008 R2 Management Studio Express 嘿,伙计们 在这里的C#相当新。我正试图学习C#ADO.NET教程(目前在step上),我被难住了。我正在遵循所有步骤,其中的所有内容对我来说都是有意义的,但每当我尝试调试时,WebApplication1的Default.aspx页面中都没有显示任何内容(在c#methods的意义上,它没有将Northwind数据库中的表打印到
WebApplication1
的Default.aspx页面中都没有显示任何内容(在c#methods的意义上,它没有将Northwind数据库中的表打印到我的网页上)
有一段时间,我认为这是我的连接字符串,conn
,我没有命名“数据源”
属性,据我所知,这是我试图连接到的服务器的名称。这一切都在本地计算机上,我正在输入正确的服务器名称。。我想。服务器名称为AZUES-221\JDOESQLSERVER
我正在正确地避开反斜杠,但我仍然不知道。我的代码中是否有缺陷?请帮忙
C#代码
提前感谢,因为您的示例似乎是一个Web项目请尝试将代码放入
页面加载
事件处理程序中。之后,您应该尝试将数据打印到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
}
}
}
}
您可能会发现查看或仅使用另一种类型的项目(如winForm、console等)非常有用因为您的示例似乎是一个Web项目尝试将代码放在
页面加载
事件处理程序中。之后,您应该尝试将数据打印到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
}
}
}
}
您可能会发现查看或仅使用另一种类型的项目(例如winForm、console等)非常有用。为什么console.writeline会显示任何内容。您没有在控制台上工作
以防万一,只是为了查看您的输出。使用Response.writeline(rdr[0]) 为什么console.writeline会显示任何内容。您没有在控制台上工作
以防万一,只是为了查看您的输出。使用Response.writeline(rdr[0]) 创建一个控制台应用程序,而不是您创建的Web应用程序。 否则,考虑到您是C#(或者一般来说是Visual Studio)新手,并且考虑到本教程的其余部分大量使用Console.WriteLine,您将遇到类似的问题 然后可以使用与教程中所示相同的代码 此外,如果您担心数据库服务器中的斜杠(它是数据库服务器实例),您可能需要尝试以下方法:
SqlConnection conn = new SqlConnection(@"Server=AZUES-221\JDOESQLSERVER;Database=Northwind;Trusted_Connection=True;");
来源:创建一个控制台应用程序,而不是您创建的Web应用程序。 否则,考虑到您是C#(或者一般来说是Visual Studio)新手,并且考虑到本教程的其余部分大量使用Console.WriteLine,您将遇到类似的问题 然后可以使用与教程中所示相同的代码 此外,如果您担心数据库服务器中的斜杠(它是数据库服务器实例),您可能需要尝试以下方法:
SqlConnection conn = new SqlConnection(@"Server=AZUES-221\JDOESQLSERVER;Database=Northwind;Trusted_Connection=True;");
来源:尝试使用
Debug.WriteLine
而不是控制台。WriteLine
,没有附加到web应用程序的控制台。不,它位于系统中。Diagnostics
命名空间中,点击Ctrl+。(Ctrl和点键)在调试字之后,使用弹出窗口添加所需的using指令。尝试使用Debug.WriteLine
而不是Console.WriteLine
,没有控制台连接到web应用程序。不,它在系统中。诊断命名空间中,点击Ctrl+。(Ctrl和点键)并使用弹出窗口添加所需的using指令。