如何在三层架构中使用存储过程插入数据? [英] How to insert data by using store procedure in three tier architecture?
本文介绍了如何在三层架构中使用存储过程插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用3层架构使用ASP .Net开发简单的学生管理系统。我已将数据层放在APP Code文件夹下的db_connection.cs文件中,以及充当业务逻辑层的其他类文件。
db_connection.cs的代码是
I am developing simple student management system using ASP .Net by using 3 tier architecture. I have placed my data layer in db_connection.cs file under APP Code folder along with other class files those are acting as the business logic layer.
the code of db_connection.cs is
public class db_connection
{
public SqlConnection con;
public SqlDataAdapter adp;
public DataSet ds = new DataSet();
public SqlCommand cmd;
public db_connection()
{
}
public void connection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ds"].ConnectionString);
con.Open();
}
public int inupdel(string sql)
{
connection();
cmd = new SqlCommand();
return cmd.ExecuteNonQuery();
}
}
And the students_details.cs file code --
public class student_details
{
db_connection db = new db_connection();
public student_details()
{
}
public int insert(string name, string address, string city, string state, int pin, int dept_id, DateTime doa, int coursefee)
{
db.cmd = new SqlCommand("spCreateStudentDetails");
db.cmd.CommandType = CommandType.StoredProcedure;
db.cmd.Parameters.AddWithValue("@StudentName", name);
db.cmd.Parameters.AddWithValue("@StudentAddress", address);
db.cmd.Parameters.AddWithValue("@StudentCity", city);
db.cmd.Parameters.AddWithValue("@StudentState", state);
db.cmd.Parameters.AddWithValue("@StudentPIN", pin);
db.cmd.Parameters.AddWithValue("@StudentDeptID", dept_id);
db.cmd.Parameters.AddWithValue("@StudentAddmissionDate", doa);
db.cmd.Parameters.AddWithValue("@StudentCourseFee", coursefee);
student_details s = new student_details();
// return db.inupdel("insert into StudentDetails values ('"+name+"','"+address+"','"+city+"', '"+state+"','"+pin+"','"+dept_id+"','"+doa+"','"+coursefee+"')");
}
}
当我返回sql时,它正在工作很好,但无法理解如何将存储过程返回到db_connection.cs文件中的inupdel()。请帮忙。在此先感谢。
When ever I am returning the sql it's working fine, but could not able to understand how to return the store procedure to the inupdel() in the db_connection.cs file. Please help. Thanks in advance.
推荐答案
我没有测试以下内容,但我认为这个想法很明确:
I didn't test the following, but I think the idea is clear:
using(var conn = new SqlConnection(connStr)) {
using(var cmd = new SqlCommand(storedProcedureName, conn) {
cmd.CommandType = CommandType.StoredProcedure;
// parameters
return cmd.ExecuteNonQuery();
}
}
我只需要在student_details中传递exec spname .cs的inupdel()。
IE返回db.inupdel(exec spCre ateStudentDetails< parameters>)。而已。感谢@Vedat Ozan Oner的回复。
I just need to pass exec spname inside student_details.cs's inupdel().
I.E. return db.inupdel("exec spCreateStudentDetails <parameters>"). That's it. Thanks @Vedat Ozan Oner for your reply.
这篇关于如何在三层架构中使用存储过程插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文