c# -为什么我得到红色下划线错误
本文关键字:红色 下划线 错误 为什么 | 更新日期: 2023-09-27 18:13:00
我是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)
{
}
}
}
SqlConnectionDemo
类中的代码,需要在一个方法中。
你应该查一下Using
语句,因为它处理了很多你正在做的清理工作。
同样,你通常不会在asp.net应用程序中使用Console.WriteLine
;
代码也必须在一个方法中,你不能像那样直接将代码放入一个类中。遵循以下结构
namespace a
{
class b
{
public int c()
{
//Your code here
}
}
}
call like so
var d = new b().c();
您不能将代码语句直接放在类的主体上,您必须将其插入方法中,因此,例如,我将连接代码放在Button1单击事件中:
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");
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)`enter code here`
{
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();
}
}
}
}
}
Update: Doh,误读了原始代码,问题是代码在类区域而不是在方法中:
public class SqlConnectionDemo
{
// NEED METHOD DECLARATION HERE
public void SomeMethod()
{
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();
}
}
}
}
Update顺便说一句,如果你想要的只是一个try/finally来确保你关闭/处理连接和读取器,并让异常冒泡,使用using
块代替。using
块将在其中列出的变量上调用Dispose()
,无论它是否正常退出作用域或抛出异常,它也是空安全的。它只是try/finally
块的一个方便的缩写,用于实现IDisposable
:
public void SomeMethod()
{
using (var conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"))
{
conn.Open();
using (var cmd = new SqlCommand("select * from Customers", conn))
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
}
}
更新:根据你的问题,如果你有一个名为_resultsText
的结果文本框,它被定义为多行,你可以这样做:
public void SomeMethod()
{
using (var conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"))
{
conn.Open();
using (var cmd = new SqlCommand("select * from Customers", conn))
using (var rdr = cmd.ExecuteReader())
{
var builder = new StringBuilder();
while (rdr.Read())
{
builder.Append(rdr[0]).Append(Environment.NewLine);
}
// assuming you are on the main thread here, if you're calling
// from a parallel thread you spawned, you'd need to check if
// invoke required, lot's of SO answers on how-to, etc if so.
_resultsText.Text = builder.ToString();
}
}
}