如何编写程序将存储过程添加到SQL Server数据库中? [英] How to write program to add stored procedures into SQL server database?

查看:88
本文介绍了如何编写程序将存储过程添加到SQL Server数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用c#开发了一个桌面应用程序,它运行良好;我使用存储过程进行数据库通信。我写了20个存储过程在应用程序中使用。在客户端安装应用程序时,他们已经拥有了我在app中使用的相同数据库。现在我只需将所有程序添加到他们的数据库中,但添加20个程序是一项非常耗时的任务。所以我想写一个简单的程序,只添加程序到选定的数据库。怎么写这种类型的程序?



Plz提示一些想法。



我是什么尝试过:



我尝试了一些代码,但没有得到确切的想法

I developed a desktop application in c# and it works nice; and i used stored procedures for database communications. I written 20 stored procedures to use in application. While installing application at client side they already have same database which i used in my app.Now i just have to add all procedures into their database, but adding 20 procedures is such a time consuming task . So i want to write a simple program which only add procedures to selected database. how to write this type of program?

Plz suggest some ideas.

What I have tried:

I tried some code but not getting exact idea

推荐答案

你需要为那些存储过程生成DROP和CREATE SQL脚本。如果有脚本,可以直接在客户或数据库上运行它们,或者如果需要在安装期间创建过程,则可以从应用程序内部调用脚本。即使您正在使用Entity Framework,您也可以创建与数据库的连接,并将SQL脚本作为SQL字符串传递给执行命令。



生成SQL脚本,打开SQL IDE,选择数据库,选择存储过程文件夹,然后按F7。程序列表显示在右侧。选择你的20个proc,右键单击并选择generate SQL Scripts。



祝你好运
You need to generate the DROP and CREATE SQL Scripts for those Stored procedures. When you have the scripts, you could either run them directly on the customers or database or if you need the procedures to be created during installation, you can call the scripts from inside your application. Even if you are using Entity Framework you can just create a connection to the database and pass the SQL Script into an execute command as a SQL string.

To generate the SQL Scripts, open your SQL IDE, select your database, select the stored procedures folder and press F7. The list of procedures are displayed on the right. Select your 20 procs, right click and choose generate SQL Scripts.

good luck


你只需使用生成脚本功能在SSMS中为所有SP创建单个文件。然后,你只需要在实际的数据库上运行它。



你可以找到一步一步的过程其他 [ ^ ]。
You simply make use of generate scripts feature in SSMS and create single file for all the SPs. Then, you just need to run it on the actual database.

You can find step by step process to do it here[^] on MSDN.


打开SqlConnection,创建一个SqlCommand对象。

将命令文本设置为CREATE PROC字符串: CREATE PROCEDURE(Transact-SQL) [ ^ ]

Open an SqlConnection, create an SqlCommand object.
Set the command text to the CREATE PROC string: CREATE PROCEDURE (Transact-SQL)[^]
CREATE PROC myProcName   
AS   
BEGIN
   SELECT custName FROM customers;
END


这篇关于如何编写程序将存储过程添加到SQL Server数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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