如何从C#codebehind SQL脚本文件? [英] how to generate sql script file from C# codebehind?

查看:136
本文介绍了如何从C#codebehind SQL脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#codebehind SQL脚本文件?该脚本文件将包含创建下拉在DB一storedproc的声明。

How to generate sql script file from C# codebehind? The script file will contain create and drop statement of a storedproc in the db.

如何从数据库在C#中生成脚本,写一个.sql文件?

How to generate the script from db in C# and write to an .sql file?

是否有可能产生的脚本来创建并从数据库在C#中删除表或storedproc声明

Is it possible to generate script of create and drop statement of table or storedproc from db in C#

请帮忙。

推荐答案

如果我已经正确地理解,建立了一个的SqlCommand 对象具有以下

If i've understood correctly, set up a SqlCommand object with the following

using (SqlConnection con = new SqlConnection ("Connection String Here"))
{
    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandText = "sp_helptext @procName";
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("procName", "Name Of Stored Proc Here");

        con.Open(); 

        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                /* 
                    You will get the CREATE PROC text here
                    Do what you need to with it. For example, write
                    to a .sql file
                */
            }
        }
    }
}

这将返回一个存储过程的文本作为 CREATE PROC 语句,在结果集,其中的每一行是存储过程的一条线。

This will return the text of a stored procedure as a CREATE PROC statement, in a result set where each row of the resultset is a line of the stored procedure.

要创建一个 DROP PROC 语句,只需写出

To create a DROP PROC statement, simply write out

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'procName') AND type in (N'P', N'PC'))
DROP PROC procName

的.sql 文件。

这篇关于如何从C#codebehind SQL脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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