二进制类型的列 -1 表示 MAX 值
Column of Binary Type -1 for MAX value
我正在构建一个 VS 解决方案并使用 BIML,我已经创建了层和 c# 代码文件。
当我 运行 每个单独的 biml 文件时,它们会编译并在查看器中生成输出。
当我检查错误时,它会抛出这个错误
"二进制类型的列必须指定正长度或-1来表示最大值。
在我的一个 c# 代码文件中,我正在做一个关于数据类型的 case 语句以切换到 SQL 数据类型。
在此代码页中,我指定二进制列的长度为 -1,但我仍然收到错误。
任何帮助,将不胜感激。
我尝试将 -1 更改为 10 和 1,但仍然出现相同的错误。
DataRow.cs 文件内容
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Varigence.Biml.Extensions;
public static class DataRow
{
public static string GetBiml(this System.Data.DataRow dataRow)
{
StringBuilder biml = new StringBuilder("");
biml.Append("<Column Name=\"")
.Append(dataRow["ColumnName"])
.Append("\" DataType=\"")
.Append(dataRow["DataTypeBiml"])
.Append("\"");
if (dataRow["DataTypeBiml"].ToString().Contains("String"))
biml.Append(" Length=\"").Append(dataRow["CharLength"]).Append("\"");
else if (dataRow["DataTypeBiml"] == "Decimal")
biml.Append(" Precision=\"").Append(dataRow["NumericPrecision"]).Append("\" Scale=\"").Append(dataRow["NumericScale"]).Append("\"");
else if (dataRow["DataTypeBiml"] == "Binary")
biml.Append(" Length=\"-1 \" ");
if (dataRow["IsNullable"] != "NO")
biml.Append(" IsNullable=\"true\"");
else
biml.Append(" IsNullable=\"false\"");
biml.Append(" />");
return biml.ToString();
}
}
1-ReadMetaData.biml
<#@ template tier="10" #>
<#@ import namespace="System.Data"#>
<#@ import namespace="System.Data.SqlClient"#>
<#@ code file="Helper.cs" #>
<#@ code file="DataRow.cs" #>
<#
string targetConnection = @"Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;";
#>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Tables>
<#
var sourceTables = Helper.GetIncludedSourceTablesList();
// Loop through each source table in the included source tables list
foreach (Table sourceTable in sourceTables)
{
#>
<Table Name="<#=sourceTable.Name#>" SchemaName="schema">
<#
string targetQuery = @"SELECT OrdinalPosition = col.ORDINAL_POSITION,
ColumnName = col.COLUMN_NAME,
DataType = col.DATA_TYPE,
CharLength = ISNULL(col.CHARACTER_MAXIMUM_LENGTH, 0),
NumericPrecision = col.NUMERIC_PRECISION,
NumericScale = col.NUMERIC_SCALE,
IsNullable = col.IS_NULLABLE,
DataTypeBiml = CASE col.DATA_TYPE
WHEN 'bigint' THEN 'Int64'
WHEN 'bit' THEN 'Boolean'
WHEN 'char' THEN 'AnsiStringFixedLength'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'Decimal'
WHEN 'float' THEN 'Double'
WHEN 'int' THEN 'Int32'
WHEN 'nchar' THEN 'StringFixedLength'
WHEN 'nvarchar' THEN 'String'
WHEN 'smallint' THEN 'Int16'
WHEN 'timestamp' THEN 'Binary'
WHEN 'tinyint' THEN 'Byte'
WHEN 'varchar' THEN 'AnsiString'
WHEN 'uniqueidentifier' THEN 'Guid'
ELSE 'Unknown'
END
FROM (
SELECT lkup.TABLE_SCHEMA,
lkup.TABLE_NAME,
ORDINAL_POSITION_MAX = MAX(lkup.ORDINAL_POSITION)
FROM INFORMATION_SCHEMA.COLUMNS AS lkup
WHERE lkup.TABLE_SCHEMA = 'dbo'
AND lkup.TABLE_NAME = '" + sourceTable.Name + @"'
GROUP BY lkup.TABLE_SCHEMA,
lkup.TABLE_NAME
) AS maxord
INNER JOIN INFORMATION_SCHEMA.COLUMNS AS col ON (maxord.TABLE_SCHEMA = col.TABLE_SCHEMA
AND maxord.TABLE_NAME = col.TABLE_NAME)
ORDER BY col.ORDINAL_POSITION;";
DataTable targetTable = new DataTable();
SqlDataAdapter targetAdapter = new SqlDataAdapter(targetQuery,targetConnection);
targetAdapter.Fill(targetTable);
#>
<Columns>
<# foreach (DataRow targetRow in targetTable.Rows) {#>
<#=targetRow.GetBiml()#>
<# } #>
</Columns>
</Table>
<# } #>
</Tables>
</Biml>
TableList.cs 文件内容
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Varigence.Biml.Extensions;
public class Helper
{
public static List<Table> GetIncludedSourceTablesList()
{
var tablesList = new List<Table>
{
new Table() { Name = "Tab1"},
new Table() { Name = "Tab2" },
new Table() { Name = "Tab3" },
new Table() { Name = "Tab4" },
new Table() { Name = "Tab5" },
new Table() { Name = "Tab6" }
};
return tablesList;
}
}
public class Table
{
public string Name { get; set; }
}
这是 ReadMetaData.biml 文件查看器中的输出部分,未将长度与二进制列相对应
<Column Name="RowVers" DataType="Binary" IsNullable="true" />
看看你的代码,它可能很简单,因为你在 -1
之后有一个尾随 space,这可能会丢弃 Length
属性 作为它有一个无效值:
所以这个:
biml.Append(" Length=\"-1 \" ");
应该变成这样:
biml.Append(" Length=\"-1\" ");
我弄清楚了错误,我没有将 DataTypeBiml 转换为字符串来比较它,一旦我把长度正确地输出了。
else if (dataRow["DataTypeBiml"].ToString() == "Binary")
biml.Append(" Length=\"-1\"");
感谢您的建议
我正在构建一个 VS 解决方案并使用 BIML,我已经创建了层和 c# 代码文件。 当我 运行 每个单独的 biml 文件时,它们会编译并在查看器中生成输出。 当我检查错误时,它会抛出这个错误 "二进制类型的列必须指定正长度或-1来表示最大值。
在我的一个 c# 代码文件中,我正在做一个关于数据类型的 case 语句以切换到 SQL 数据类型。 在此代码页中,我指定二进制列的长度为 -1,但我仍然收到错误。 任何帮助,将不胜感激。
我尝试将 -1 更改为 10 和 1,但仍然出现相同的错误。
DataRow.cs 文件内容
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Varigence.Biml.Extensions;
public static class DataRow
{
public static string GetBiml(this System.Data.DataRow dataRow)
{
StringBuilder biml = new StringBuilder("");
biml.Append("<Column Name=\"")
.Append(dataRow["ColumnName"])
.Append("\" DataType=\"")
.Append(dataRow["DataTypeBiml"])
.Append("\"");
if (dataRow["DataTypeBiml"].ToString().Contains("String"))
biml.Append(" Length=\"").Append(dataRow["CharLength"]).Append("\"");
else if (dataRow["DataTypeBiml"] == "Decimal")
biml.Append(" Precision=\"").Append(dataRow["NumericPrecision"]).Append("\" Scale=\"").Append(dataRow["NumericScale"]).Append("\"");
else if (dataRow["DataTypeBiml"] == "Binary")
biml.Append(" Length=\"-1 \" ");
if (dataRow["IsNullable"] != "NO")
biml.Append(" IsNullable=\"true\"");
else
biml.Append(" IsNullable=\"false\"");
biml.Append(" />");
return biml.ToString();
}
}
1-ReadMetaData.biml
<#@ template tier="10" #>
<#@ import namespace="System.Data"#>
<#@ import namespace="System.Data.SqlClient"#>
<#@ code file="Helper.cs" #>
<#@ code file="DataRow.cs" #>
<#
string targetConnection = @"Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;";
#>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Tables>
<#
var sourceTables = Helper.GetIncludedSourceTablesList();
// Loop through each source table in the included source tables list
foreach (Table sourceTable in sourceTables)
{
#>
<Table Name="<#=sourceTable.Name#>" SchemaName="schema">
<#
string targetQuery = @"SELECT OrdinalPosition = col.ORDINAL_POSITION,
ColumnName = col.COLUMN_NAME,
DataType = col.DATA_TYPE,
CharLength = ISNULL(col.CHARACTER_MAXIMUM_LENGTH, 0),
NumericPrecision = col.NUMERIC_PRECISION,
NumericScale = col.NUMERIC_SCALE,
IsNullable = col.IS_NULLABLE,
DataTypeBiml = CASE col.DATA_TYPE
WHEN 'bigint' THEN 'Int64'
WHEN 'bit' THEN 'Boolean'
WHEN 'char' THEN 'AnsiStringFixedLength'
WHEN 'datetime' THEN 'DateTime'
WHEN 'decimal' THEN 'Decimal'
WHEN 'float' THEN 'Double'
WHEN 'int' THEN 'Int32'
WHEN 'nchar' THEN 'StringFixedLength'
WHEN 'nvarchar' THEN 'String'
WHEN 'smallint' THEN 'Int16'
WHEN 'timestamp' THEN 'Binary'
WHEN 'tinyint' THEN 'Byte'
WHEN 'varchar' THEN 'AnsiString'
WHEN 'uniqueidentifier' THEN 'Guid'
ELSE 'Unknown'
END
FROM (
SELECT lkup.TABLE_SCHEMA,
lkup.TABLE_NAME,
ORDINAL_POSITION_MAX = MAX(lkup.ORDINAL_POSITION)
FROM INFORMATION_SCHEMA.COLUMNS AS lkup
WHERE lkup.TABLE_SCHEMA = 'dbo'
AND lkup.TABLE_NAME = '" + sourceTable.Name + @"'
GROUP BY lkup.TABLE_SCHEMA,
lkup.TABLE_NAME
) AS maxord
INNER JOIN INFORMATION_SCHEMA.COLUMNS AS col ON (maxord.TABLE_SCHEMA = col.TABLE_SCHEMA
AND maxord.TABLE_NAME = col.TABLE_NAME)
ORDER BY col.ORDINAL_POSITION;";
DataTable targetTable = new DataTable();
SqlDataAdapter targetAdapter = new SqlDataAdapter(targetQuery,targetConnection);
targetAdapter.Fill(targetTable);
#>
<Columns>
<# foreach (DataRow targetRow in targetTable.Rows) {#>
<#=targetRow.GetBiml()#>
<# } #>
</Columns>
</Table>
<# } #>
</Tables>
</Biml>
TableList.cs 文件内容
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Varigence.Biml.Extensions;
public class Helper
{
public static List<Table> GetIncludedSourceTablesList()
{
var tablesList = new List<Table>
{
new Table() { Name = "Tab1"},
new Table() { Name = "Tab2" },
new Table() { Name = "Tab3" },
new Table() { Name = "Tab4" },
new Table() { Name = "Tab5" },
new Table() { Name = "Tab6" }
};
return tablesList;
}
}
public class Table
{
public string Name { get; set; }
}
这是 ReadMetaData.biml 文件查看器中的输出部分,未将长度与二进制列相对应
<Column Name="RowVers" DataType="Binary" IsNullable="true" />
看看你的代码,它可能很简单,因为你在 -1
之后有一个尾随 space,这可能会丢弃 Length
属性 作为它有一个无效值:
所以这个:
biml.Append(" Length=\"-1 \" ");
应该变成这样:
biml.Append(" Length=\"-1\" ");
我弄清楚了错误,我没有将 DataTypeBiml 转换为字符串来比较它,一旦我把长度正确地输出了。
else if (dataRow["DataTypeBiml"].ToString() == "Binary")
biml.Append(" Length=\"-1\"");
感谢您的建议