2013-07-06 32 views
0

我對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文件了嗎?

+0

'使用SqlComm;'不應該'使用SqlDataClient' ..? – MethodMan

+0

也許您需要將構建操作設置爲在App_Code文件夾中的.cs文件中進行編譯。除此之外,您應該能夠直接訪問該類和它的方法。 –

+0

@Tim Schmelter我是否需要爲網站做到這一點,我認爲這隻適用於網絡應用程序?我已經看過如何在Visual Web Developer中這樣做的說明,並且當我右鍵單擊文件 – Cineno28

回答

1

using SQLComm意味着你要使用的命名空間SqlComm,它不存在。我想你想要什麼(在你的代碼隱藏的某個地方):

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

@D赤柱謝謝你的幫助!我試過這一行:DataTable dt = new DataTable(); dt = SqlComm.SqlDataTable(「SELECT * FROM Users WHERE UserName ='demo'」);現在我得到一個錯誤,「SqlComm在當前上下文中不存在」 – Cineno28

+0

請確保您有一個對SqlComm存在的項目的引用(如果在不同的項目中),並用類命名空間,因爲這是做到這一點的方法。 – SimpleVar

+0

@Yorye Nathan所以它必須在名稱空間內?從D斯坦利的說明中,我認爲既然沒有命名空間,我可以直接調用它?我將它包裝在名爲「SqlComm」的名稱空間中,並將「使用SqlComm」放回到代碼隱藏中,但它仍然給我一個錯誤。回答你的其他問題,是的,它在同一個項目 – Cineno28

1

你可以直接調用它。只有 鑑於你的類有靜態方法,你會這樣稱呼它:

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

謝謝你的幫助!我試過上述代碼返回到一個對象,我仍然收到一個錯誤,說「CS0103:名稱'SqlComm'在當前上下文中不存在」 – Cineno28

0

所以我相信我想通了。我在我的Aspx網站的子目錄中有我的App_Code文件夾,而不是在服務器的根目錄中。我將該文件夾移到了根目錄,現在它正在工作。

雖然我有興趣聽到我的編輯的任何答案,但我原來的帖子。既然我是以網站而不是網絡應用程序的形式來做這件事,那麼在使用類似這樣的類或其他問題時,我是否還需要記住其他任何問題?我環顧四周,但沒有看到很多差異。

感謝大家的幫助!

相關問題