SQL Server UDT 和存储过程从 .Net 代码修剪十进制数字值

我们正在使用 .net 框架 4.7.2。我们调用一个带有用户定义类型变量的 sp 作为它的唯一参数。


CREATE TYPE [dbo].[ABC] AS TABLE(

[A] [int] NOT NULL,

[B] [datetime] NOT NULL,

[C] [datetime] NOT NULL,

[Value] [decimal](19, 6) NULL)

对应的存储过程是


CREATE PROCEDURE [dbo].[myUSP]

@data dbo.ABC readonly AS

BEGIN

SET NOCOUNT ON;

IF EXISTS (SELECT 1 FROM @data)

BEGIN

      INSERT INTO dbo.MyTable

      SELECT A, B, C, [Value] FROM @data;

END END

我的 .Net 代码是


            using (SqlConnection con = new SqlConnection(connectionString))

            {

                    using (SqlCommand insertCmd = new SqlCommand("dbo.myUSP", con))

                    {

                        con.Open();

                        using (transaction = con.BeginTransaction(IsolationLevel.RepeatableRead))

                        {

                            insertCmd.Transaction = transaction;

                            insertCmd.CommandType = CommandType.StoredProcedure;

                            try

                            {

                                SqlParameter parameter1 = insertCmd.Parameters.AddWithValue("@data", CreateSqlRecord(insert));

                                parameter1.SqlDbType = SqlDbType.Structured;

                                parameter1.TypeName = "dbo.ABC";

                                insertCmd.ExecuteNonQuery();

                                transaction.Commit();

                            }

                            catch (Exception ex)

                            {

                                transaction.Rollback();

                            }

                        }

                    }

            }


我检查了 parameter1 的值,然后才将其传递给 SQLConnection 并使用 ExecuteNonQuery 执行,并且它正确包含十进制值。另一方面,我还检查了直接从 SQL 服务器管理工作室运行我的 sp,它在表 dbo.MyTable 中插入了正确的十进制值。


BEGIN DECLARE @data dbo.ElementFactData;INSERT @data (ElementId,StartDateTime, EndDateTime, Value) VALUES(  1002, '1/1/1800' , '1/1/1900' , 0.786); exec dbo.myUSP @data;END

但是,当我尝试从 .net 代码中插入记录时,小于 0.5 的十进制值变为 0,而大于 0.5 的值变为 1。如 4.2 变为 4,5.87 变为 6


我的 .net 代码有什么问题吗?


郎朗坤
浏览 161回答 1
1回答

慕丝7291255

我猜可能是&nbsp; metaData[3] = new SqlMetaData("Value", SqlDbType.Decimal);需要用精度和比例指定。所以它看起来像这样:&nbsp; metaData[3] = new SqlMetaData("Value", SqlDbType.Decimal, 19, 6);您的 CreateSqlRecord 方法应该如下所示:private IEnumerable<SqlDataRecord> CreateSqlRecord(IEnumerable<DataElementInput> entities){&nbsp; &nbsp; SqlMetaData[] metaData = new SqlMetaData[4];&nbsp; &nbsp; metaData[0] = new SqlMetaData("A", SqlDbType.Int);&nbsp; &nbsp; metaData[1] = new SqlMetaData("B", SqlDbType.DateTime);&nbsp; &nbsp; metaData[2] = new SqlMetaData("C", SqlDbType.DateTime);&nbsp; &nbsp; metaData[3] = new SqlMetaData("Value", SqlDbType.Decimal, 19, 6);&nbsp; &nbsp; SqlDataRecord record = new SqlDataRecord(metaData);&nbsp; &nbsp; foreach (Model myModel in entities)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; record.SetInt32(0, myModel .A);&nbsp; &nbsp; &nbsp; &nbsp; record.SetDateTime(1,myModel.B);&nbsp; &nbsp; &nbsp; &nbsp; record.SetDateTime(2, myModel.C);&nbsp; &nbsp; &nbsp; &nbsp; record.SetDecimal(3, (Decimal)myModel.Value);&nbsp; &nbsp; &nbsp; &nbsp; yield return record;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP