Yah*_*hoo 0 c# asp.net visual-studio-2010 visual-studio
我是VS2010和C#的新手.
我的这段代码给了我很多红色下划线.它有些愚蠢的错误,但无法弄清楚它是什么.这是守则."conn","try""rdr"都是红色下划线,为什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2
{ public class SqlConnectionDemo
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
类中的代码SqlConnectionDemo需要在方法中.
您应该查看UsingStatement,因为它会处理您在清理后所做的很多事情.
此外,您通常不会Console.WriteLine在asp.net应用程序中使用; 它适用于控制台应用程序.
| 归档时间: |
|
| 查看次数: |
2286 次 |
| 最近记录: |