在 ASP .NET 中通过 C# 获取存储过程脚本
Getting stored procedure script through C# in ASP .NET
我正在开发 DbCompre 工具。基本上我的要求是通过 C# 在 web 应用程序中生成存储过程脚本。首先可以吗?
在 SQL 服务器中,我们可以通过右键单击或借助命令 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'
但是在数据库中这个过程是可用的。
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
*/
}
}
}
}
绝对有可能,您需要将 SP 名称作为参数传递给参数 objname
:-
SqlCommand cmd= new SqlCommand("sys.sp_helptext", AppCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "dloc_GetTasksRewarehousePutaway");
我正在开发 DbCompre 工具。基本上我的要求是通过 C# 在 web 应用程序中生成存储过程脚本。首先可以吗?
在 SQL 服务器中,我们可以通过右键单击或借助命令 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'
但是在数据库中这个过程是可用的。
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
*/
}
}
}
}
绝对有可能,您需要将 SP 名称作为参数传递给参数 objname
:-
SqlCommand cmd= new SqlCommand("sys.sp_helptext", AppCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "dloc_GetTasksRewarehousePutaway");