如何获取 C# SMO 中 ExecuteWithResults 返回的 DataSet 中列的 MaxLength?
How to get the MaxLength of a Column in a DataSet returned by ExecuteWithResults in C# SMO?
我需要获取在 ExecuteWithResults 方法中执行的任何 SELECT 中返回的任何列的最大长度。
示例:
Table TbCli 有列 NmCli (varchar NOT NULL)
using System;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
...
Server server = new Server();
server.ConnectionContext.ServerInstance = "ServerInstance";
server.ConnectionContext.LoginSecure = true;
server.ConnectionContext.DatabaseName = "DatabaseName";
server.ConnectionContext.Connect();
DataSet lQR = server.ConnectionContext.ExecuteWithResults("Select NmCli From TbCli");
var max = lQR.Tables[0].Columns[0].MaxLength;
Console.WriteLine("\n- End!"); Console.ReadKey();
在本例中,max 变量被赋值为 -1。
我需要一些能给我数字 50 的东西。(因为在这个例子中,该列是 varchar(50))
记住:
这是一个例子。如果有人将此查询发送到我的方法:
SELECT a.ID, a.Name, b.ID, b.Name From TableA as a inner join TableB as b on a.BID = b.ID
我需要能够获得所有列的正确长度。
提前致谢,
使用exec sp_help 'tablename'
。它会 return 多个结果集,第二个会给你列的长度。
编辑:
下面的代码块应该给你你想要的。根据您的评论,我不确定有什么复杂之处,但我能够创建一个包含列名和列长度的字典。
Server server = new Server();
server.ConnectionContext.ConnectionString = @"ConnectionStringHere";
server.ConnectionContext.Connect();
var ColumnDictionary = new Dictionary<string, int>();
using (SqlDataReader lQR = server.ConnectionContext.ExecuteReader("sp_help 'dbo.WhicheverTable'"))
{
lQR.NextResult();
while (lQR.Read())
{
ColumnDictionary.Add((String)lQR.GetValue(0),(Int32)lQR.GetValue(3));
}
}
//Do anything with the dictionary here
Console.WriteLine("\n- End!"); Console.ReadKey();
我需要获取在 ExecuteWithResults 方法中执行的任何 SELECT 中返回的任何列的最大长度。
示例:
Table TbCli 有列 NmCli (varchar NOT NULL)
using System;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
...
Server server = new Server();
server.ConnectionContext.ServerInstance = "ServerInstance";
server.ConnectionContext.LoginSecure = true;
server.ConnectionContext.DatabaseName = "DatabaseName";
server.ConnectionContext.Connect();
DataSet lQR = server.ConnectionContext.ExecuteWithResults("Select NmCli From TbCli");
var max = lQR.Tables[0].Columns[0].MaxLength;
Console.WriteLine("\n- End!"); Console.ReadKey();
在本例中,max 变量被赋值为 -1。 我需要一些能给我数字 50 的东西。(因为在这个例子中,该列是 varchar(50))
记住:
这是一个例子。如果有人将此查询发送到我的方法:
SELECT a.ID, a.Name, b.ID, b.Name From TableA as a inner join TableB as b on a.BID = b.ID
我需要能够获得所有列的正确长度。
提前致谢,
使用exec sp_help 'tablename'
。它会 return 多个结果集,第二个会给你列的长度。
编辑: 下面的代码块应该给你你想要的。根据您的评论,我不确定有什么复杂之处,但我能够创建一个包含列名和列长度的字典。
Server server = new Server();
server.ConnectionContext.ConnectionString = @"ConnectionStringHere";
server.ConnectionContext.Connect();
var ColumnDictionary = new Dictionary<string, int>();
using (SqlDataReader lQR = server.ConnectionContext.ExecuteReader("sp_help 'dbo.WhicheverTable'"))
{
lQR.NextResult();
while (lQR.Read())
{
ColumnDictionary.Add((String)lQR.GetValue(0),(Int32)lQR.GetValue(3));
}
}
//Do anything with the dictionary here
Console.WriteLine("\n- End!"); Console.ReadKey();