【发布时间】:2018-12-16 17:35:47
【问题描述】:
我在 SQL Server Management Studio 中编写了这个查询,用于获取数据库中存储过程的定义。
Select [definition], [uses_ansi_nulls], [uses_quoted_identifier],
[is_schema_bound], [uses_database_collation], [is_recompiled],
[null_on_null_input], [execute_as_principal_id],
[uses_native_compilation]
From [sys].[sql_modules]
Where [object_id] = @object_Id
在 SSMS 中,如果我 Declare @object_Id int = <storedproc's object_id> 并运行它,我会返回定义值(SQL 代码)和各种其他属性。
在我的应用程序中,我有这段代码。上面的 SQL 查询是项目的资源。
static void RenderDefinition(int objectId, XmlElement parentElement)
{
var dt = new DataTable();
using (var da = new SqlDataAdapter(Resources.Commands.GetDefinition, connectionString))
{
da.SelectCommand.Parameters.Add("@object_Id", SqlDbType.Int).Value = objectId;
da.Fill(dt);
}
var row = dt.Rows[0];
// Create object element
var e = parentElement.AppendChild(parentElement.OwnerDocument.CreateElement("definition")) as XmlElement;
foreach (DataColumn col in dt.Columns)
{
var value = row.ToXmlString(col);
if (string.IsNullOrWhiteSpace(value)) continue;
if (col.ColumnName == "definition")
{
// The SQL code
e.InnerText = value; // <-- Value is always DBNull, never the actual value
}
else
{
// Other defining attributes
e.SetAttribute(col.ColumnName, value);
}
}
}
这会生成以下 XML:
<definition uses_ansi_nulls="true" uses_quoted_identifier="true" is_schema_bound="false" uses_database_collation="false" is_recompiled="false" null_on_null_input="false" uses_native_compilation="false"></definition>
我已经按原样尝试了查询。我尝试将定义列转换为 VARCHAR(MAX) 和 VARCHAR(8000)。
有什么想法吗?
【问题讨论】:
-
bigint不是int... -
@ZoharPeled 感谢您的捕获,尽管它不会改变行为,因为它已本地化为 SSMS。
-
调试时,
dt.Rows.Count返回什么? -
Resources.Commands.GetDefinition的值是多少? -
可能是权限问题,虽然不能确定。查看docs.microsoft.com/en-us/sql/relational-databases/…
标签: c# sql-server tsql ado.net