C# Dapper自定义帮助程序(用于表值参数)浮点值中的逗号而不是点
来自的代码非常有用 但是MSSQL SQL_变量列通常会带来麻烦C# Dapper自定义帮助程序(用于表值参数)浮点值中的逗号而不是点,c#,sql-server-2008,dapper,C#,Sql Server 2008,Dapper,来自的代码非常有用 但是MSSQL SQL_变量列通常会带来麻烦 private static string ConnectionString = @"data source=192.168.1.1;initial catalog=;persist security info=False;user id=sa;password="; private static void TestTVP2() { var connection = new SqlConnection(C
private static string ConnectionString = @"data source=192.168.1.1;initial catalog=;persist security info=False;user id=sa;password=";
private static void TestTVP2()
{
var connection = new SqlConnection(ConnectionString);
connection.Open();
var nums =
connection
.Query<BigIntKeySqlVariantValueType.TypeFields>("[dbo].[pmesAttributeValueUpdate]", new BigIntKeySQLVariantValueTVP(new[]
{
new BigIntKeySqlVariantValueType.TypeFields{ParameterName = 9976669, ParameterValue= 8.5}
}, "@tvp")
).ToList();
foreach (var num in nums)
{
Console.WriteLine(num.BigIntKeySqlVariantValueTypeId + " " + num.ParameterName + " " +
num.ParameterValue);
}
}
internal class BigIntKeySQLVariantValueTVP : SqlMapper.IDynamicParameters
{
private readonly IEnumerable<BigIntKeySqlVariantValueType.TypeFields> _values;
private readonly string _parametername;
public BigIntKeySQLVariantValueTVP(IEnumerable<BigIntKeySqlVariantValueType.TypeFields> values, string parametername)
{
this._values = values;
this._parametername = parametername;
}
public void AddParameters(IDbCommand command)
{
var sqlCommand = (SqlCommand)command;
sqlCommand.CommandType = CommandType.StoredProcedure;
var numberList = new List<SqlDataRecord>();
// Create an SqlMetaData object that describes our table type.
SqlMetaData[] tvpDefinition =
{
new SqlMetaData(
"BigIntKeySQLVariantValueTypeId", SqlDbType.BigInt, true, false, SortOrder.Unspecified, -1),
new SqlMetaData("ParameterName", SqlDbType.BigInt),
new SqlMetaData("ParameterValue", SqlDbType.Variant),
};
foreach (var n in _values)
{
// Create a new record, using the metadata array above.
var rec = new SqlDataRecord(tvpDefinition);
rec.SetInt64(1, n.ParameterName); // Set the value.
rec.SetValue(2, n.ParameterValue);
numberList.Add(rec); // Add it to the list.
}
// Add the table parameter.
var p = sqlCommand.Parameters.Add(_parametername, SqlDbType.Structured);
p.Direction = ParameterDirection.Input;
p.TypeName = "BigIntKeySQLVariantValueType";
p.Value = numberList;
}
public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
AddParameters(command);
}
}
public class BigIntKeySqlVariantValueType
{
/// <summary>
/// Type POCO
/// </summary>
public struct TypeFields
{
public long BigIntKeySqlVariantValueTypeId { get; private set; }
public long ParameterName { get; set; }
public object ParameterValue { get; set; }
}
}
我的类型:
Dapper生成的代码应为:
insert into @p1 values(9976669,8.5)
如何在SqlParameter中用点替换逗号
事实证明,逗号而不是句点的值只显示在SQL事件探查器和Console.Write中
将正确的浮点值传递给数据库。如何获得该输出?您正在使用SQL跟踪吗?或者是类似于迷你剖析器的东西?关键是:dapper从不添加declare等-参数作为参数发送。这里的错误可能只是在渲染过程中未使用固定区域性时发生的UI错误。Dapper输出是Profiler中的Control+C并调用:connection.Query[dbo]。[pmesAttributeValueUpdate],tvp.ToList;必须生成插入,对吗?很难说;这可能只是一个分析器渲染问题;它现在能用吗?另外:您没有显示您提供给dapper的命令/参数,因此我很难评论它是否正确/错误。。。能否显示查询/执行的调用?包括命令/command-type/etc-Dapper并没有添加INSERT-etc。它只是添加了参数。@marcGravel它可以工作。。。“天空剖面仪”和“地球控制台”中的所有迹象都表明不应该这样,因此我没有检查表中是否有数据。附加问题:为什么SQL Profiler显示插入以及实际的SQL语句是什么样子。我认为这是一个善意的谎言,目的是使其易于阅读,并使复制/粘贴到SSM中进行调试成为可能
CREATE TYPE [dbo].[BigIntKeySQLVariantValueType] AS TABLE(
[BigIntKeySQLVariantValueTypeId] [bigint] IDENTITY(1,1) NOT NULL,
[ParameterName] [bigint] NULL,
[ParameterValue] [sql_variant] NULL,
PRIMARY KEY CLUSTERED
(
[BigIntKeySQLVariantValueTypeId] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
insert into @p1 values(9976669,8.5)
// Add the table parameter.
var p = sqlCommand.Parameters.Add(_parametername, SqlDbType.Structured);
p.Direction = ParameterDirection.Input;
p.TypeName = "BigIntKeySQLVariantValueType";
p.Value = numberList;