2015-10-06 71 views
-1

我正在使用DbCompre工具。基本上我的要求是通过C#在Web应用程序中生成存储过程脚本。首先是可能的?通过ASP中的C#获取存储过程脚本

在SQL Server中,我们通过右键单击或通过命令sp_helptext <ProcedureName>轻松生成脚本。

这里是我的代码:

public DataTable generateScripts() 
{ 
     SqlCommand cmd = new SqlCommand("sys.sp_helptext dloc_GetTasksRewarehousePutaway", AppCon); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter adp = new SqlDataAdapter(cmd); 
     adp.Fill(DS); 
     adp.SelectCommand = cmd; 
     return DS.Tables[0]; 
} 

当代码执行我收到此错误:

Could not find stored procedure 'sys.sp_helptext dloc_GetTasksRewarehousePutaway'

但在数据库中此过程中是可用的。

+0

什么连接路径,你在安美设置? – Mainak

+0

连接字符串完美工作,因为我在整个应用程序中使用了相同的连接字符串。 – Anjyr

回答

3

绝对的可能,你需要通过SP名作为参数,虽然在参数objname: -

SqlCommand cmd= new SqlCommand("sys.sp_helptext", AppCon); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@objname", "dloc_GetTasksRewarehousePutaway"); 
+1

谢谢。它工作正常。 – Anjyr

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

     cmd.Parameters.AddWithValue("procName", "dloc_GetTasksRewarehousePutaway"); 

     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 
       */ 
      } 
     } 
    } 
}