如何使用存储过程进行面向对象编程 [英] How to do object oriented programming using stored procedures

查看:139
本文介绍了如何使用存储过程进行面向对象编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#中面向对象的programmin的新手。我们被要求仅使用存储过程进行编程。可以通过使用一个简单的Windows窗体应用程序来表达如何做到这一点。你可以请说明如何进行插入操作作为一个例子。

I'm new to object oriented programmin in C#. We are asked to use only the stored procedures to program. Can tou please explin how to do it by using a simple windows form application.Can you please explin how to do the insert operation as an example.

推荐答案

我们可以'真的那样做。存储过程是SQL,而不是C#,而Sql没有面向对象编程的概念 - 它的SP是严格程序性的,而不是面向对象的。



那么我们怎么能给出你是那个描述中的任何一个例子吗?
We can't really do that. Stored Procedures are SQL, not C#, and Sql has no concept of Object Oriented Programming - it's SPs are strictly procedural, rather than object oriented.

So how could we give you any example from that description?


你可以先为每个可以创建C#方法的程序创建程序。它被称为映射。你可以看下面的例子



创建程序prcAddCategory

@catName nvarchar(50)



插入类别(CategoryName)值(@catName)



并且对于此过程,您可以在下面创建C#方法来执行过程



public void AddCategory(string categoryName)

{

SqlConnection conn = new SqlConnection(yourConnectionString);

SqlCommand cmd = new SqlCommand(prcAddCategory,conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue(@ catName,categoryName );

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();



}



如果您有错误,我可以查看...
You can create procedures first after that for every procedure you can create C# methods. It is called Mapping. You can look at the example below

Create Procedure prcAddCategory
@catName nvarchar(50)
as
Insert into Categories(CategoryName) values (@catName)

and for this procedure you can create the C# method below to execute procedure

public void AddCategory(string categoryName)
{
SqlConnection conn = new SqlConnection(yourConnectionString);
SqlCommand cmd = new SqlCommand(prcAddCategory,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@catName,categoryName);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

}

If you have error,I can check...


遗产使用客户端 - 服务器模型构建的应用程序,其中存储过程需要将大部分业务域逻辑重新设计为n层架构,以便您可以获得/获得OO设计的好处。
Legacy applications built using a client-server model where the stored procedures have much of the business domain logic need to be redesigned to an n-tier architecture so that you can get/reap the benefit of OO designs.


这篇关于如何使用存储过程进行面向对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆