从后面的代码调用类

时间:2013-07-06 22:46:25

标签: c# asp.net

我还是ASP.net的新手,我正在学习如何调用类。我一直在寻找教程,但并非所有都是特定于ASP.net 4.0所以我不确定是否应该应用它们。

现在,我正在尝试连接到我的SQL数据库。我的webconfig文件已经使用connectionstring“dbConnectionString”设置,并且在我使用GridViews进行测试时正常工作。我现在要做的是从后面的代码访问数据库。

我已经看到了一些方法来实现这一目标,但我想要最有效,最灵活的方式。我试图采用此处列出的答案:How to create sql connection with c# code behind, access the sql server then conditionally redirect?但是,我收到了错误。

我使用C#作为网站,而不是Web应用程序。这是我的Login.aspx.cs背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SqlComm; // Is this how I connect to my class from this page????


public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //no code written yet
        }
    }

}

我的App_Code文件夹中的类,文件名为SQLComm.cs:

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

/// <summary>
/// SQL Query class
/// </summary>
public class SqlComm
{
    // Connection string
    static string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

    // Execute sql command with no value to return
    public static void SqlExecute(string sql)
    {
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
        }
    }

    // Execute SQL query and return a value
    public static object SqlReturn(string sql)
    {
        using (SqlConnection conn = new SqlConnection(PjSql.dbcs()))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            object result = (object)cmd.ExecuteScalar();
            return result;
        }
    }

    // Retrieve an entire table or part of it
    public static DataTable SqlDataTable(string sql)
    {
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Connection.Open();
            DataTable TempTable = new DataTable();
            TempTable.Load(cmd.ExecuteReader());
            return TempTable;
        }
    }

    // Execute a stored procedure with 1 parameter
    // Returning a value or just executing with no returns
    public static object SqlStoredProcedure1Param(string StoredProcedure, string PrmName1, object Param1)
    {
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd = new SqlCommand(StoredProcedure, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter(PrmName1, Param1.ToString()));
            cmd.Connection.Open();
            object obj = new object();
            obj = cmd.ExecuteScalar();
            return obj;
        }
    }
}

正如您所看到的,我还没有编写任何代码来实际使用该类,但是“使用SQLComm;” line本身给了我这个错误:

Compiler Error Message: CS0246: The type or namespace name 'SqlComm' could not be found (are you missing a using directive or an assembly reference?)

由于我还是新手,我不确定从哪里开始。我似乎在上面链接的页面上包含了答案中包含的所有内容。还有什么我想念的?

编辑:我在关于类似问题的帖子中读到了这篇文章:asp.NET 2.0 Web Site Can't Access Classes in App_Code 这可能是因为我正在做一个网站而不是一个Web应用程序,所以当我将文件FTP过来时,App_Code文件夹的处理方式会有所不同吗?

3 个答案:

答案 0 :(得分:1)

using SQLComm表示您要使用不存在的名称空间 SqlComm。我想你想要的是(在你的代码隐藏的某个地方):

DataTable dt = SqlComm.SqlDataTable(...)   // call one of the static methods.

答案 1 :(得分:1)

您可以直接拨打电话。 鉴于你的类只有静态方法,你可以这样称呼它:

SqlComm.SqlReturn("select * from table");

答案 2 :(得分:0)

所以我相信我弄明白了。我的App_Code文件夹位于我的Aspx网站的子目录中,而不是在服务器的根目录中。我将文件夹移动到根目录,现在它正在工作。

我有兴趣听到我对原始帖子的编辑的任何答案。由于我这样做是作为一个网站而不是一个Web应用程序,我是否还需要记住有关使用此类或其他问题的类的其他问题?我环顾四周,但没有看到很多不同。

感谢大家的帮助!