比较数据 table 和 SQL table
Compare datatable and SQL table
所以我用谷歌搜索...有没有一种简单的方法可以将 SQL table 的 table 结构与 CLR 数据 table 进行比较?
问题是:我们有一个 API,return 和 JSON,它在不断发展。当 return 数组包含新对象时,我们想通知调用者有更多数据可用。
我们可以通过查询轻松获得 SQL 架构:
select COLUMN_NAME,ORDINAL_POSITION
from information_schema.columns
where table_name = 'ApiWork'
但是我们如何比较 column/ordinal 和包含 JSON 数组的 DataTable?
典型的 return 数组如下所示:
{"Index_0":"930477","Index_1":"test789","ArrayID":"1","Result":"OK","Order_ID":"930477","Model_Number":"FGHB2868TF","Ship_Date":"05/30/2018","Allocated":0,"Backordered":1,"Amount":0}
我们可以使用 json 反序列化器或循环构建数据 table:
DataTable dt = new DataTable();
SqlPipe pipe = SqlContext.Pipe;
String d = "";
String col = "";
int l = 0;
int l2 = 0;
int s = 0;
int s2 = 0;
o = "{\"Index_0\":\"930477\",\"Index_1\":\"test789\",\"ArrayID\":\"1\",\"Result\":\"OK\",\"DMI_Order_ID\":\"930477\",\"Model_Number\":\"FGHB2868TF\",\"Ship_Date\":\"05/30/2018\",\"Allocated\":0,\"Backordered\":1,\"Amount\":0}";
int c = o.Length;
while (c > 0)
{
col = o.Substring(0, o.IndexOf(":")).Replace("\"", "").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "");
dt.Columns.Add(col);
l = o.IndexOf(":");
l = l + 1;
s = o.Length - l;
o = o.Substring(l,s); // here we have removed the name portion
l2 = o.IndexOf(",");
l2 = l2 + 1;
s2 = o.Length - l2;
o = o.Substring(l2, s2); // here we have removed the value of the previous name
c = o.Length;
if (o.IndexOf(":") == -1 && o.IndexOf(",") == -1)
{
c = 0;
}
}
我想如果有必要,这样的循环也可以控制序数,但正如我在其中一条评论中提到的那样,这并不是完全必要的。
所以这看起来很简单:
using (SqlCommand command = new SqlCommand("Select name from tempdb.sys.COLUMNS Where object_id=OBJECT_ID('tempdb.dbo.#ApiWork')EXCEPT Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where table_name = 'ApiWork'"))
{
command.Connection = connection;
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
pipe.Send("Something is different!");
}else{
pipe.Send("we're all good!");
}
}
}
可以检查这两个查询以查看列是否不同。顺便说一句
COLUMN_ID
和
ORDINAL_ID
可以添加到查询中,然后也可以检查。
所以我用谷歌搜索...有没有一种简单的方法可以将 SQL table 的 table 结构与 CLR 数据 table 进行比较?
问题是:我们有一个 API,return 和 JSON,它在不断发展。当 return 数组包含新对象时,我们想通知调用者有更多数据可用。
我们可以通过查询轻松获得 SQL 架构:
select COLUMN_NAME,ORDINAL_POSITION
from information_schema.columns
where table_name = 'ApiWork'
但是我们如何比较 column/ordinal 和包含 JSON 数组的 DataTable? 典型的 return 数组如下所示:
{"Index_0":"930477","Index_1":"test789","ArrayID":"1","Result":"OK","Order_ID":"930477","Model_Number":"FGHB2868TF","Ship_Date":"05/30/2018","Allocated":0,"Backordered":1,"Amount":0}
我们可以使用 json 反序列化器或循环构建数据 table:
DataTable dt = new DataTable();
SqlPipe pipe = SqlContext.Pipe;
String d = "";
String col = "";
int l = 0;
int l2 = 0;
int s = 0;
int s2 = 0;
o = "{\"Index_0\":\"930477\",\"Index_1\":\"test789\",\"ArrayID\":\"1\",\"Result\":\"OK\",\"DMI_Order_ID\":\"930477\",\"Model_Number\":\"FGHB2868TF\",\"Ship_Date\":\"05/30/2018\",\"Allocated\":0,\"Backordered\":1,\"Amount\":0}";
int c = o.Length;
while (c > 0)
{
col = o.Substring(0, o.IndexOf(":")).Replace("\"", "").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "");
dt.Columns.Add(col);
l = o.IndexOf(":");
l = l + 1;
s = o.Length - l;
o = o.Substring(l,s); // here we have removed the name portion
l2 = o.IndexOf(",");
l2 = l2 + 1;
s2 = o.Length - l2;
o = o.Substring(l2, s2); // here we have removed the value of the previous name
c = o.Length;
if (o.IndexOf(":") == -1 && o.IndexOf(",") == -1)
{
c = 0;
}
}
我想如果有必要,这样的循环也可以控制序数,但正如我在其中一条评论中提到的那样,这并不是完全必要的。
所以这看起来很简单:
using (SqlCommand command = new SqlCommand("Select name from tempdb.sys.COLUMNS Where object_id=OBJECT_ID('tempdb.dbo.#ApiWork')EXCEPT Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where table_name = 'ApiWork'"))
{
command.Connection = connection;
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
pipe.Send("Something is different!");
}else{
pipe.Send("we're all good!");
}
}
}
可以检查这两个查询以查看列是否不同。顺便说一句
COLUMN_ID
和
ORDINAL_ID
可以添加到查询中,然后也可以检查。