[英]calling a stored procedure to a class in visual studio
我編寫了一個簡單的存儲過程,將sp_insert插入數據,然后使用sp_Update來更新數據,然后使用sp_delete刪除數據,然后使用sp_search搜索數據,最后使用sp_view將數據查看到sql server中,如下所示 。 我測試了存儲過程並執行良好。現在我已經將數據庫Icon連接到我的Vs2015,直到單擊測試連接成功為止,然后單擊Uk,現在它已連接。 看起來像這些..
現在,我們在VS2015 C#中使用Windows窗體UI。 我在我的項目的解決方案資源管理器中創建了一個命名為存儲庫的類。這些類在這里有一個代碼。這是我的問題,我無法調用四個存儲過程,即sp_insert,sp_Update,sp_delete,我班上的sp_search,sp_view沒什么可顯示的。關於我的代碼是否有任何問題。
public class repositories
{
//Initializes a new instance of the DataContext class by referencing the connection name of database.
public static DataClasses1DataContext db = null;
//that registration is my table
public static void add(registration)
{
db = new DataClasses1DataContext();
//they are getting red cannot call sp_insert that i was created in sql server in my whole entire procedure.
db.sp_insert(Username, password, securityQuestion, SecurityAnswer, LastName, firstName, MiddleInitial, address, gender, birthdate, age);
}
public static void Update (registration)
{
db = new DataClasses1DataContext();
db.sp_Update(Username, password, securityQuestion, SecurityAnswer, LastName, firstName, MiddleInitial, address, gender, birthdate, age);
}
public static void delete (registration)
{
db = new DataClasses1DataContext();
db.sp_Delete(UserId);
}
public static List<sp_ViewResult> View()
{
db = new DataClasses1DataContext();
List<sp_ViewResult> View() = db.sp_view().Tolist<sp_viewResult>();
}
}
}
我需要一些指導。 誰能幫我..
感謝您的幫助。
如果您使用的是實體框架,並且注冊是一個在您的問題中未得到澄清的對象,則此代碼應該有效。
還要確保已在實體框架模型中添加了存儲過程。 如果添加了它們,則可能會出錯,因為您沒有添加任何值,而是添加了整個類。
僅供參考。 如果您正在運行調試模式,則如果不在try catch中,它將在過程中捕獲任何錯誤,但是,如果您運行release,則可能不會捕獲該錯誤。
您可能必須使用System進行導入; 使用IDisposable
public class repositories: IDisposable
{
private DataClasses1DataContext _db;
public DataClasses1DataContext db
{
get
{
if (_db == null)
{
_db = new DataClasses1DataContext();
}
return _db;
}
}
public static void add(registration reg)
{
db.sp_insert(reg.Username, reg.password, reg.securityQuestion, reg.SecurityAnswer, reg.LastName, reg.firstName, reg.MiddleInitial, reg.address, reg.gender, reg.birthdate, reg.age);
}
public static void Update(registration reg)
{
db.sp_Update(reg.Username, reg.password, reg.securityQuestion, reg.SecurityAnswer, reg.LastName, reg.firstName, reg.MiddleInitial, reg.address, reg.gender, reg.birthdate, reg.age);
}
public static void delete(registration reg)
{
db.sp_Delete(reg.UserId);
}
public static List<sp_ViewResult> View()
{
List<sp_ViewResult> View() = db.sp_view().Tolist<sp_viewResult>();
}
public void Dispose()
{
db.Dispose();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.