无法在c#(web-app)中访问我的新课程

时间:2013-11-20 09:34:40

标签: c# visual-studio-2010

您好我正在学习编程,但目前我卡住了。 当我将这个类直接添加到应该使用该类的文件中时,它可以工作。 当我把这个类en放在一个单独的.cs文件中时,我似乎无法使用它。

这是我访问我的数据库的DAL类(非常基本的)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{

    // We valideren de gegevens die de gebruiker ingeeft met de gegevens in de database.
    public static bool CheckUser(string username, string password)
    {
        DataTable result = null;
        try
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RTIdb"].ConnectionString))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT Wachtwoord FROM Gebruikers Where GebruikersNaam = @uname";
                    cmd.Parameters.Add(new SqlParameter("@uname", username));

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        result = new DataTable();
                        da.Fill(result);
                    }
                    if (password.Trim() == result.Rows[0]["Wachtwoord"].ToString().Trim())
                    {
                        // Als we hier geraken zijn de ingevoerde gebruikersnaam en wachtwoord correct

                        return true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // problem handling
        }
        // gebruikersnaam niet gevonden
        return false;
    }

    public static string GetWeergaveNaam(string username)
    {
        DataTable result = null;
        try
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RTIdb"].ConnectionString))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT WeergaveNaam FROM Gebruikers WHERE GebruikersNaam = @uname";
                    cmd.Parameters.Add(new SqlParameter("@uname", username));

                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        result = new DataTable();
                        da.Fill(result);
                    }
                    if (result.Rows.Count == 1)
                    {
                        return result.Rows[0]["WeergaveNaam"].ToString().Trim();
                    }
                }
            }
        }
        catch (Exception)
        {
            // TODO opvangen exception
        }

        return "SQL ERROR";

    }

    // Nu moeten we de rol van de gebruiker nog opzoeken
    public static string GetUserRoles(string username)
    {
        DataTable result = null;
        try
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RTIdb"].ConnectionString))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT Roles FROM Gebruikers WHERE GebruikersNaam = @uname";
                    cmd.Parameters.Add(new SqlParameter("@uname", username));

                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        result = new DataTable();
                        da.Fill(result);
                    }
                    if (result.Rows.Count == 1)
                    {
                        return result.Rows[0]["Roles"].ToString().Trim();
                    }
                }
            }
        }

        catch (Exception ex)
        {
            // exception handling
        }
        // user id not found, dus is hij een gast
        return "guest";
    }
}

现在我想在不同的文件中访问这些方法,如

DAL.CheckUser(username, password);

然后我在visual studio中得到一个错误:“在当前上下文中不存在名称'DAL'” 我会假设它有使用命名空间的东西,但我没有在任何文件中声明任何命名空间。当我添加“命名空间”声明时,它在我的第二个文件中找不到命名空间...所以换句话说我被卡住了:-s

我希望有人能把我送到正确的方向......

3 个答案:

答案 0 :(得分:1)

请再次检查项目的namespace关键字。我在两个差异文件夹中的两个文件上成功编译了两个类。

项目文件夹中的文件Business.c

public class Business
{
    public void TestFunc()
    {
        DAL.TestFunction();
    }
}

DAL文件夹中的文件DAL.cs

public class DAL
{
    public static void TestFunction()
    { 
        //Do something
    }
}

答案 1 :(得分:0)

您的类不在命名空间内。 要引用不在命名空间内的类,您应该使用:

global::DAL.CheckUser(username, password);

请注意,不要声明命名空间是一种不好的做法。

答案 2 :(得分:0)

这里有两个命名空间选项:

1)您可以在同一名称空间中声明这两个文件;

2)您可以在不同的命名空间中声明这两个文件,并在文件中声明一个using命令,该命令需要引用另一个命名空间。

命名空间将您的类分组为逻辑组。通常使用项目的文件夹结构中的类的位置。 1个命名空间中的文件可以在没有using命令的情况下调用彼此的受保护和公共成员。不同命名空间中的文件需要对另一个命名空间进行明确的using引用。