sqlconnection缺少类型或命名空间引用

时间:2015-07-09 17:50:29

标签: c#

我有一个SQL查询,我想用它来从数据库中获取结果。当我尝试打开一个sqlconnection时,我的代码将无法编译,我收到一个错误,询问我是否缺少using指令。

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


namespace HackOffice.Superadmin
{
    public partial class FoundUsBreakdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            grabData();
        }
        private void grabData()
        {
            string chartDataQuery = "select AccountType, COUNT(*) Entries from HackOffice.dbo.OnlineSignups group by AccountType";
            using (sqlconnection connection = new sqlconnection(connectionString));
        }
    }
}

我很困惑因为我在其他页面上使用了相同的指令而且我在这些页面上没有得到相同的错误。

1 个答案:

答案 0 :(得分:1)

类名在C#中区分大小写。您需要SqlConnection,而不是sqlconnection

此外,您正在创建一个空的using语句,该语句将立即处置该资源。当您打开使用块时,您将要在块中放置使用IDisposable资源的所有代码:

using (SqlConnection connection = new SqlConnection(connectionString))
{
   // Code that uses SqlConnection goes inside the block.
}
相关问题