我對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應用程序,所以App_Code文件夾被處理方式不同,當我的FTP文件了嗎?
'使用SqlComm;'不應該'使用SqlDataClient' ..? – MethodMan
也許您需要將構建操作設置爲在App_Code文件夾中的.cs文件中進行編譯。除此之外,您應該能夠直接訪問該類和它的方法。 –
@Tim Schmelter我是否需要爲網站做到這一點,我認爲這隻適用於網絡應用程序?我已經看過如何在Visual Web Developer中這樣做的說明,並且當我右鍵單擊文件 – Cineno28