【发布时间】:2013-07-06 22:46:24
【问题描述】:
我还是 ASP.net 的新手,我正在学习如何调用类。我四处寻找教程,但并非所有教程都特定于 ASP.net 4.0,所以我不确定是否应该应用它们。
现在,我正在尝试连接到我的 SQL 数据库。我的 webconfig 文件已使用连接字符串“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;”行本身给了我这个错误:
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 文件夹的处理方式不同?
【问题讨论】:
-
using SqlComm;不应该是using SqlDataClient..? -
可能需要在App_Code文件夹的.cs文件中设置编译动作。除此之外,您应该能够直接访问该类及其方法。
-
@Tim Schmelter 我是否需要为网站执行此操作,我认为这仅适用于 Web 应用程序?我已经查看了有关如何在 Visual Web Developer 中执行此操作的说明,当我右键单击文件时似乎没有该选项
-
@Cineno28:我必须承认我对网站不是很熟悉,所以这只是一个猜测。
-
@Tim Schmelter 没问题,感谢您的帮助!我在使用应用程序时看到过这个选项,但我想我应该先学习 .net 网站,然后再深入研究 Web 应用程序。