使用@parameter = value类型传递的Sql参数

时间:2016-02-11 08:43:12

标签: c# sql .net sql-server sqlparameters

我正在使用会计程序。我想在我的C#应用​​程序中实现该程序的一个功能。会计程序使用MS SQL数据库。所以我通过SQL Server Profiler查找了SQL跟踪日志。

Accounting Program将此命令发送到我想在我的应用程序中实现的数据库:

exec sp_executesql N'UPDATE "MyDB".."MyTable"
 SET "CARDREF"=@P1,"DATE_"=@P2,"MODULENR"=@P3,"SIGN"=@P4,"FICHEREF"=@P5,
"FICHELINEREF"=@P6,"TRCODE"=@P7,"TOTAL"=@P8,(....)
WHERE "LOGICALREF"=@P78 AND "CARDREF"=@P79 AND "DATE_"=@P80 AND "MODULENR"=@P81
 AND "SIGN"=@P82 AND (...),

N'@P1 int,@P2 datetime,@P3 smallint,@P4 smallint,@P5 int,@P6 int,@P7 smallint,
@P8 float,@P9 float,@P10 float,@P11 float,@P12 int,@P13 smallint,@P14 smallint (....),

12309,'2016-01-01 00:00:00',5,0,1417,0,14,1126,9000000000001,(.....)

我已将此查询实施到我的应用,但我遇到了问题。我的应用程序发送这样的参数值(来自SQL Server Profiler跟踪日志):

exec sp_executesql N'UPDATE "MyDB".."MyTable"
 SET "CARDREF"=@P1,"DATE_"=@P2,"MODULENR"=@P3,
"SIGN"=@P4,"FICHEREF"=@P5,"FICHELINEREF"=@P6,"TRCODE"=@P7,"TOTAL"=@P8,(....)
 WHERE "LOGICALREF"=@P78 AND "CARDREF"=@P79 AND "DATE_"=@P80 AND "MODULENR"=@P81
 AND "SIGN"=@P82 AND (...),

 N'@P1 int,@P2 datetime,@P3 smallint,@P4 smallint,@P5 int,@P6 int,@P7 smallint, @P8 float,
@P9 float,@P10 float,@P11 float,@P12 int,@P13 smallint,@P14 smallint (....),    


@P1=12309,@P2='2016-04-17 00:00:00',@P3=6,@P4=0,@P5=181,@P6=1068,@P7=3,@P8=5138,8999999999996,
 @P9=5138,8999999999996,@P10=0,@P11=0,@P12=11636,@P13=0,@P14=0,(...)

您会看到我的应用程序的参数值如下传递; @Parameter = value。不像会计程序。 (仅限参数值,不带@Parameter标记。)

以下是我的代码;

using (SqlCommand cmd = new SqlCommand("UPDATE"))
            {
                SqlTransaction trans = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                cmd.Transaction = trans;
                cmd.Connection = connection;


            cmd.CommandText = "UPDATE MyTable SET CARDREF=@P1,DATE_=@P2,MODULENR=@P3,
SIGN=@P4,FICHEREF=@P5,FICHELINEREF=@P6, TRCODE=@P7,TOTAL=@P8,PAID=@P9,
EARLYINTRATE=@P10,LATELYINTRATE=@P11,CROSSREF=@P12,PAIDINCASH=@P13,CANCELLED=@P1
4,PROCDATE=@P15,TRCURR=@P16,TRRATE=@P17,REPORTRATE=@P18(....)
 WHERE LOGICALREF=@P78 AND CARDREF=@P79 AND DATE_=@P80 AND MODULENR=@P81 AND      SIGN=@P82
 AND FICHEREF=@P83 AND FICHELINEREF=@P84 AND TRCODE=@P85 AND TOTAL=@P86 AND     PAID=@P87
 AND EARLYINTRATE=@P88 AND LATELYINTRATE=@P89 AND CROSSREF=@P90";

                cmd.Parameters.Add("@P1", SqlDbType.Int).Value = ptrns1.CARDREF;
                cmd.Parameters.Add("@P2", SqlDbType.DateTime).Value = ptrns1.DATE_;
                cmd.Parameters.Add("@P3", SqlDbType.SmallInt).Value = ptrns1.MODULENR;
                cmd.Parameters.Add("@P4", SqlDbType.SmallInt).Value = ptrns1.SIGN;
                cmd.Parameters.Add("@P5", SqlDbType.Int).Value = ptrns1.FICHEREF;               
                cmd.Parameters.Add("@P6", SqlDbType.Int).Value = ptrns1.FICHELINEREF;
                (......)
cmd.ExecuteNonQuery();
        }

0 个答案:

没有答案