pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='VheZL'><tr id='VheZL'><dt id='VheZL'><q id='VheZL'><span id='VheZL'><b id='VheZL'><form id='VheZL'><ins id='VheZL'></ins><ul id='VheZL'></ul><sub id='VheZL'></sub></form><legend id='VheZL'></legend><bdo id='VheZL'><pre id='VheZL'><center id='VheZL'></center></pre></bdo></b><th id='VheZL'></th></span></q></dt></tr></i><div class="9z5hbbj" id='VheZL'><tfoot id='VheZL'></tfoot><dl id='VheZL'><fieldset id='VheZL'></fieldset></dl></div>

<legend id='VheZL'><style id='VheZL'><dir id='VheZL'><q id='VheZL'></q></dir></style></legend>
<tfoot id='VheZL'></tfoot>

    <small id='VheZL'></small><noframes id='VheZL'>

    • <bdo id='VheZL'></bdo><ul id='VheZL'></ul>

        當 DataTypeCompatility 打開時,如何將 12/30/1899 參數

        How to parameterize 12/30/1899 to SQL Server native client when DataTypeCompatility is on?(當 DataTypeCompatility 打開時,如何將 12/30/1899 參數化為 SQL Server 本機客戶端?)

        1. <i id='zwf4w'><tr id='zwf4w'><dt id='zwf4w'><q id='zwf4w'><span id='zwf4w'><b id='zwf4w'><form id='zwf4w'><ins id='zwf4w'></ins><ul id='zwf4w'></ul><sub id='zwf4w'></sub></form><legend id='zwf4w'></legend><bdo id='zwf4w'><pre id='zwf4w'><center id='zwf4w'></center></pre></bdo></b><th id='zwf4w'></th></span></q></dt></tr></i><div class="7zd5dz7" id='zwf4w'><tfoot id='zwf4w'></tfoot><dl id='zwf4w'><fieldset id='zwf4w'></fieldset></dl></div>
            • <bdo id='zwf4w'></bdo><ul id='zwf4w'></ul>
                <legend id='zwf4w'><style id='zwf4w'><dir id='zwf4w'><q id='zwf4w'></q></dir></style></legend>
                  <tbody id='zwf4w'></tbody>
                • <tfoot id='zwf4w'></tfoot>

                  <small id='zwf4w'></small><noframes id='zwf4w'>

                  本文介紹了當 DataTypeCompatility 打開時,如何將 12/30/1899 參數化為 SQL Server 本機客戶端?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  短版

                  嘗試將 datetime12/30/1899 傳遞給 SQL Server,失敗,日期格式無效 - 但僅適用于本機客戶端驅動程序,并且僅在 DataTypeCompatiblity 模式下.

                  長版

                  嘗試在 ADO 中使用參數化查詢時,針對 SQL Server:

                  SELECT ?

                  我將 datetime 值參數化為 adDBTimeStamp:

                  //語言不可知,隱約似C#的偽代碼無效的測試它(){DateTime dt = new DateTime(3/15/2020");變體 v = DateTimeToVariant(dt);命令 cmd = 新命令();cmd.CommandText = "選擇?AS SomeDate";cmd.Parameters.Append(cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);連接 cn = GetConnection();cmd.Set_ActiveConnection(cn);cmd.Execute(out recordsAffected, EmptyParam, adExecuteNoRecords);}

                  當日期為 3/15/2020 時效果很好.

                  您創建了一個 VARIANT,帶有 VType,共 7 個(VT_DATE),以及一個 8 字節浮點值:

                  VARIANTInt32 vt = 7;//VT_DATE雙日期 = 0;

                  但它在 12/30/1899 失敗

                  如果我用一個特定的日期時間做同樣的測試代碼,它就會失敗:

                  void TestIt(){DateTime dt = new DateTime(12/30/1899");變體 v = DateTimeToVariant(dt);命令 cmd = 新命令();cmd.CommandText = "選擇?AS SomeDate";cmd.Parameters.Append(cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);連接 cn = GetConnection();cmd.Set_ActiveConnection(cn);cmd.Execute(out recordsAffected, EmptyParam, adExecuteNoRecords);}

                  ADO OLEDB 提供程序拋出異常(即在它甚至到達 SQL Server 之前):

                  無效的日期格式

                  但并非所有 SQL Server OLEDB 提供程序都會發生這種情況

                  在調試此問題時,我意識到并非所有 SQL Server OLEDB 提供程序都會發生這種情況.Microsoft 通常有 4 個 OLE DB Providers for SQL Server:

                  • SQLOLEDB:Microsoft OLE DB Provider for SQL Server(自 Windows 2000 起隨 Windows 一起提供)
                  • SQLNCLI:SQL Server Native Client(SQL Server 2005 附帶)
                  • SQLNCLI10:SQL Server Native Client 11.0(SQL Server 2008 附帶)
                  • SQLNCLI11:SQL Server Native Client 12.0(SQL Server 2012 附帶)
                  • MSOLEDBSQL:用于 SQL Server 的 Microsoft OLE DB 驅動程序(隨 SQL Server 2016 提供)

                  當與一些不同的提供商一起嘗試時,它確實對某些人來說效果很好:

                  • SQLOLEDB:有效
                  • SQLNCLI11(無數據類型兼容性):有效
                  • SQLNCLI11(啟用數據類型兼容性):失敗

                  數據類型兼容性?

                  是的.ActiveX 數據對象 (ADO),一個圍繞不友好 COM OLEDB API 的友好 COM 包裝器,不理解新的 datetimexmldatetime2datetimeoffset 數據類型.創建了新的 OLEDB 數據類型常量來表示這些新類型.因此,任何現有的 OLEDB 應用程序都不會理解新的常量.

                  為此,一個新的關鍵字由本機" OLE DB 驅動程序支持:

                  • DataTypeCompatibility=80

                  您可以將其添加到連接字符串中:

                  <塊引用>

                  "Provider=SQLNCLI11;數據源=螺絲刀;用戶 ID=hatguy;Password=hunter2;DataTypeCompatibility=80;"

                  這會指示 OLEDB 驅動程序僅返回首次發明 OLEDB 時存在的 OLEDB 數據類型:

                  <頭>
                  SQL Server 數據類型SQLOLEDBSQLNCLISQLNCLI
                  (w/DataTypeCompatibility=80)
                  XMLadLongVarWChar141 (DBTYPE_XML)adLongVarChar
                  日期時間adDBTimeStampadDBTimeStampadDBTimeStamp
                  datetime2adVarWCharadDBTimeStampadVarWChar
                  日期時間偏移adVarWChar146 (DBTYPE_DBTIMESTAMPOFFSET)adVarWChar
                  日期adVarWCharadDBDateadVarWChar
                  時間adVarWChar145 (DBTYPE_DBTIME2)adVarWChar
                  UDT132 (DBTYPE_UDT)adVarBinary(已記錄,未經測試)
                  varchar(max)adLongVarCharadLongVarCharadLongVarChar
                  nvarchar(max)adLongVarWCharadLongVarWCharadLongVarWChar
                  varbinary(max)adLongVarBinaryadLongVarBinaryadLongVarBinary
                  時間戳adBinaryadBinaryadBinary

                  失敗了

                  什么時候:

                  • 嘗試參數化 datetime
                  • 值為 12/30/1899
                  • 使用本地客戶端"驅動程序時
                  • DataTypeCompatilibty 已開啟
                  • 驅動程序本身因值而窒息
                  • 當它的價值實際上非常好時.

                  嘗試使用12/30/1899"這個日期并沒有本質上的錯誤:

                  • SELECT CAST('18991230' AS datetime) 工作正常
                  • 它在原始 OLE DB 驅動程序中運行良好
                  • 它在本機" OLE DB 驅動程序中運行良好
                  • 它只是在帶有 DataTypeCompatibility 的本機驅動程序中失敗

                  顯然這是 Microsoft OLE DB 驅動程序中的錯誤.但微軟永遠不會、永遠永遠永遠修復這個錯誤,這是一個絕對的事實.>

                  那么如何解決這個問題?

                  我可以檢測到這個特殊的日期時間,我可以嘗試解決我們數據訪問層中的這個錯誤.

                  • 但我需要一個可以放入VARIANT結構的值,
                  • 表示12/30/1899 12:00:00 AM
                  • SQOLEDB
                  • 下工作
                  • SQLNCLIxx 驅動程序
                  • MSOLEDBSQL驅動程序
                  • DataTypeCompatibilityMode
                  • (這到底是怎么回事,即使關閉了模式 - 雖然在沒有打開它的情況下使用 ADO 是無效的)

                  驅動生成的T-SQL

                  當 OLE DB 驅動程序確實真正按照我說的去做時,我們可以分析生成的 RPC:

                  SQOLEDB

                  <塊引用>

                  exec sp_executesql N'SELECT @P1 AS SomeDate',N'@P1 datetime','1899-12-30 00:00:00'

                  SQLNCLI11

                  <塊引用>

                  exec sp_executesql N'SELECT @P1 AS SomeDate',N'@P1 datetime2(0)','1899-12-30 00:00:00'

                  CMRE(德爾福)

                  program Project1;{$APPTYPE 控制臺}{$R *.res}用途System.SysUtils,對象,ActiveX,數據庫,注意,變體;函數 GetConnection(Provider: string; DataTypeCompatibility: Boolean): _Connection;無功連接字符串:字符串;開始{SQLOLEDB - Windows 的默認提供程序SQLNCLI11 - SQL Server 2008 本機客戶端}connectionString := 'Provider='+Provider+';數據源=螺絲刀;用戶ID=氫;密碼=hunter2;';如果 DataTypeCompatibility 那么connectionString := connectionString+'DataTypeCompatibility=80';結果:= CoConnection.Create;Result.Open(connectionString, '', '', adConnectUnspecified);結尾;過程測試(提供者名稱:字符串;數據類型兼容性:布爾值);無功dt: 日期時間;v:OleVariant;cmd:_命令;cn:_連接;受影響的記錄:OleVariant;s:字符串;開始dt := EncodeDate(1899, 12, 30);//12/30/1899 12:00:00 AM(在 Delphi 中也稱為零)v := dt;//該變體的類型為 VT_DATE (7)cmd := CoCommand.Create;cmd.CommandText := '選擇 ?AS SomeDate';cmd.Parameters.Append(cmd.CreateParameter('', adDBTimeStamp, adParamInput, 0, v));嘗試cn := GetConnection(ProviderName, DataTypeCompatibility);除了在 E: 例外做開始WriteLn('Provider '+ProviderName+' 未安裝: '+E.message);出口;結尾;結尾;如果 SameText(ProviderName, 'SQLOLEDB') 那么s := ''否則如果 DataTypeCompatibility thens := '(具有數據類型兼容性)'別的s := '(沒有數據類型兼容性)';cmd.Set_ActiveConnection(cn);嘗試cmd.Execute({out}recordsAffected, EmptyParam, adExecuteNoRecords);WriteLn('Provider '+ProviderName+s+': 成功.');除了在 E:Exception 上做開始WriteLn('Provider '+ProviderName+s+' 失敗:'+E.Message);結尾;結尾;結尾;程序主要;開始CoInitialize(nil);測試('SQLOLEDB',假);//自 2000 年以來隨 Windows 提供的 SQL Server 客戶端測試('SQLNCLI',假);//SQL Server 2005 原生客戶端測試('SQLNCLI',真);//SQL Server 2005 本機客戶端,具有數據類型兼容性測試('SQLNCLI10',假);//SQL Server 2008 原生客戶端測試('SQLNCLI10',真);//SQL Server 2008 本機客戶端,具有數據類型兼容性測試('SQLNCLI11',假);//SQL Server 2012 原生客戶端測試('SQLNCLI11',真);//SQL Server 2012 本機客戶端,具有數據類型兼容性測試('MSOLEDBSQL',假);//SQL Server 2016 原生客戶端測試('MSOLEDBSQL',真);//SQL Server 2016 本機客戶端,具有數據類型兼容性結尾;開始嘗試主要的;除了在 E: 例外做Writeln(E.ClassName, ': ', E.Message);結尾;WriteLn('按回車鍵關閉');讀Ln;結尾.

                  雖然這不是特定于 Delphi 的問題;我正在使用德爾福.所以它被標記為Delphi.如果你抱怨我會噎死你的舌頭.

                  <塊引用>

                  注意:這不是 ADO.net,而是 ADO.它不是托管的 .NET Framework 類庫,而是本機 Win32 COM OLE DB API.

                  解決方案

                  BrakNicku 給出了答案.

                  <塊引用>

                  將參數的 NumericScale 屬性設置為 1-7 范圍內的任何值.

                  更改代碼:

                  參數 p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);

                  參數 p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);p.NumericScale = 1;

                  有效.

                  它甚至適用于 SQL Server 2000 的 SQLOLEDB 驅動程序.

                  不同數據類型的精度和規模

                  從包含不同數據類型的SQL Server返回行集,我可以問OLEDB各種T-SQL數據類型的PrecisionNumericScale是什么:

                  SQL Server 類型 ADO 類型 Precision NumericScale DefinedSize---------------- --------------------- --------- ----————————————int adInteger (3) 10 255 4真實廣告單 (4) 7 255 4貨幣 adCurrency (6) 19 255 8位 adBoolean (11) 255 255 2tinyint adUnsignedTinyInt (17) 3 255 1bigint adBigInt (20) 19 255 8唯一標識符 adGUID (72) 255 255 16char(35) adChar (129) 255 255 35nchar(35) adWChar (130) 255 255 35十進制(15,5)和數字(131)15 5 19日期時間 adDBTimeStamp (135) 23 3 16varchar(35) adVarChar (200) 255 255 35文本 adLongVarChar (201) 255 255 2147483647varchar(max) adLongVarChar (201) 255 255 2147483647nvarchar(35) adVarWChar (202) 255 255 35nvarchar(max) adLongVarWChar (203) 255 255 1073741823xml adLongVarWChar (203) 255 255 1073741823圖像 adLongVarBinary (205) 255 255 2147483647varbinary(max) adLongVarBinary (205) 255 255 2147483647

                  因為 SQL Server 返回一個 datetime 字段,NumericScale3可能改變是一種美德:

                  參數 p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);p.NumericScale = 1;

                  參數 p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);p.NumericScale = 3;

                  獎勵閱讀

                  永遠不要、永遠永遠嘗試將datetime參數化為adDBTimestamp.Microsoft 的 SQL Server OLEDB 驅動程序中存在數據丟失錯誤(所有這些錯誤):

                  • SQLOLEDB (1999) - 失敗
                  • SQLNCLI (2005) - 失敗
                  • SQLNCLI10 (2008) - 失敗
                  • SQLNCLI11 (2010) - 失敗
                  • MSOLEDBSQL (2012) - 失敗

                  正確答案是將所有 datetime 值參數化為字符串(例如 adVarChar) 使用 ODBC 24 小時格式":

                  • yyyy-mm-dd hh:mm:ss.zzz
                  • 2021-03-21 18:16:22.619

                  Short Version

                  Trying to pass the datetime value 12/30/1899 to SQL Server, fails with Invalid date format - but only for the native client drivers, and only in DataTypeCompatiblity mode.

                  Long Version

                  When trying to use parameterized queries in ADO, against SQL Server:

                  SELECT ?
                  

                  I parameterize the datetime value as an adDBTimeStamp:

                  //Language agnostic, vaguely C#-like pseudo-code
                  void TestIt()
                  {
                     DateTime dt = new DateTime("3/15/2020");
                     VARIANT v = DateTimeToVariant(dt);
                  
                     Command cmd = new Command();
                     cmd.CommandText = "SELECT ? AS SomeDate";
                     cmd.Parameters.Append(cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                     
                     Connection cn = GetConnection();
                     cmd.Set_ActiveConnection(cn);
                     cmd.Execute(out recordsAffected, EmptyParam, adExecuteNoRecords);
                  }
                  

                  And that works fine when the date is 3/15/2020.

                  You create a VARIANT, with a VType of 7 (VT_DATE), and a value that is an 8-byte floating point value:

                  VARIANT
                     Int32  vt = 7; //VT_DATE
                     Double date = 0;
                  

                  But it fails on 12/30/1899

                  If I do the same test code with one particular datetime, it fails:

                  void TestIt()
                  {
                     DateTime dt = new DateTime("12/30/1899");
                     VARIANT v = DateTimeToVariant(dt);
                  
                     Command cmd = new Command();
                     cmd.CommandText = "SELECT ? AS SomeDate";
                     cmd.Parameters.Append(cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                     
                     Connection cn = GetConnection();
                     cmd.Set_ActiveConnection(cn);
                     cmd.Execute(out recordsAffected, EmptyParam, adExecuteNoRecords);
                  }
                  

                  The ADO OLEDB provider throws an exception (i.e. before it even reaches SQL Server):

                  Invalid date format
                  

                  But it doesn't happen with all SQL Server OLEDB providers

                  When debugging this issue, I realized it doesn't happen with all of the SQL Server OLEDB providers. Microsoft generally has 4 OLE DB Providers for SQL Server:

                  • SQLOLEDB: Microsoft OLE DB Provider for SQL Server (has shipped with Windows since Windows 2000)
                  • SQLNCLI: SQL Server Native Client (shipped with SQL Server 2005)
                  • SQLNCLI10: SQL Server Native Client 11.0 (shipped with SQL Server 2008)
                  • SQLNCLI11: SQL Server Native Client 12.0 (shipped with SQL Server 2012)
                  • MSOLEDBSQL: Microsoft OLE DB Driver for SQL Server (shipped with SQL Server 2016)

                  When trying it with some different providers, it does work fine for some:

                  • SQLOLEDB: Works
                  • SQLNCLI11 (without DataTypeCompatibility): Works
                  • SQLNCLI11 (with DataTypeCompatiility on): Fails

                  DataTypeCompatibility?

                  Yes. ActiveX Data Objects (ADO), a friendly COM wrapper around the unfriendly COM OLEDB API, doesn't understand the new date, time, xml, datetime2, datetimeoffset data types. New OLEDB data type constants were created to represents these new types. So any existing OLEDB applications wouldn't understand the new constants.

                  To that end, a new keyword is supported by the "native" OLE DB drivers:

                  • DataTypeCompatibility=80

                  which you can add to your connection string:

                  "Provider=SQLNCLI11; Data Source=screwdriver; User ID=hatguy; Password=hunter2;DataTypeCompatibility=80;"

                  This instructs the OLEDB driver to only return OLEDB data types that were in existance when OLEDB was first invented:

                  SQL Server data type SQLOLEDB SQLNCLI SQLNCLI
                  (w/DataTypeCompatibility=80)
                  Xml adLongVarWChar 141 (DBTYPE_XML) adLongVarChar
                  datetime adDBTimeStamp adDBTimeStamp adDBTimeStamp
                  datetime2 adVarWChar adDBTimeStamp adVarWChar
                  datetimeoffset adVarWChar 146 (DBTYPE_DBTIMESTAMPOFFSET) adVarWChar
                  date adVarWChar adDBDate adVarWChar
                  time adVarWChar 145 (DBTYPE_DBTIME2) adVarWChar
                  UDT 132 (DBTYPE_UDT) adVarBinary (documented,untested)
                  varchar(max) adLongVarChar adLongVarChar adLongVarChar
                  nvarchar(max) adLongVarWChar adLongVarWChar adLongVarWChar
                  varbinary(max) adLongVarBinary adLongVarBinary adLongVarBinary
                  timestamp adBinary adBinary adBinary

                  And there's the failure

                  When:

                  • trying to parameterize a datetime value
                  • with a value of 12/30/1899
                  • when using a "native client" driver
                  • and DataTypeCompatilibty is on
                  • the driver itself chokes on the value
                  • when its value is, in fact perfectly fine.

                  There's nothing inherently wrong with trying to use a date of '12/30/1899`:

                  • SELECT CAST('18991230' AS datetime) works fine
                  • it works fine in the original OLE DB driver
                  • it works fine in the "native" OLE DB drivers
                  • it just fails in the native driver with DataTypeCompatibility on

                  Obviously this is a bug in Microsoft OLE DB drivers. But it's an absolute truth that Microsoft will never, ever, ever, EVER, fix the bug.

                  So how to work-around it?

                  I can detect this special datetime, and I can try to work around this bug in our data access layers.

                  • But I need a value I can place into a VARIANT structure,
                  • that represents 12/30/1899 12:00:00 AM
                  • that works under SQOLEDB
                  • and under SQLNCLIxx drivers
                  • and under MSOLEDBSQL driver
                  • in DataTypeCompatibilityMode
                  • (and what the hell, even with the mode off - although it's invalid to use ADO without it on)

                  T-SQL generated by the driver

                  When the OLE DB driver does bother to actually do what i say, we can profile the RPC generated:

                  SQOLEDB

                  exec sp_executesql N'SELECT @P1 AS SomeDate',N'@P1 datetime','1899-12-30 00:00:00'

                  SQLNCLI11

                  exec sp_executesql N'SELECT @P1 AS SomeDate',N'@P1 datetime2(0)','1899-12-30 00:00:00'

                  CMRE (Delphi)

                  program Project1;
                  
                  {$APPTYPE CONSOLE}
                  
                  {$R *.res}
                  
                  uses
                    System.SysUtils,
                    ComObj,
                    ActiveX,
                    ADOdb,
                    ADOint,
                    Variants;
                  
                  function GetConnection(Provider: string; DataTypeCompatibility: Boolean): _Connection;
                  var
                      connectionString: string;
                  begin
                  {
                      SQLOLEDB - Default provider with Windows
                      SQLNCLI11 - SQL Server 2008 native client
                  }
                      connectionString := 'Provider='+Provider+'; Data Source=screwdriver;User ID=hydrogen;Password=hunter2;';
                      if DataTypeCompatibility then
                          connectionString := connectionString+'DataTypeCompatibility=80';
                  
                      Result := CoConnection.Create;
                      Result.Open(connectionString, '', '', adConnectUnspecified);
                  end;
                  
                  procedure Test(ProviderName: string; DataTypeCompatibility: Boolean);
                  var
                      dt: TDateTime;
                      v: OleVariant;
                      cmd: _Command;
                      cn: _Connection;
                      recordsAffected: OleVariant;
                      s: string;
                  begin
                      dt := EncodeDate(1899, 12, 30);// 12/30/1899 12:00:00 AM (also known in Delphi as zero)
                      v := dt; //the variant is of type VT_DATE (7)
                  
                      cmd := CoCommand.Create;
                      cmd.CommandText := 'SELECT ? AS SomeDate';
                      cmd.Parameters.Append(cmd.CreateParameter('', adDBTimeStamp, adParamInput, 0, v));
                  
                      try
                          cn := GetConnection(ProviderName, DataTypeCompatibility);
                      except
                          on E: Exception do
                              begin
                                  WriteLn('Provider '+ProviderName+' not installed: '+E.message);
                                  Exit;
                              end;
                      end;
                  
                      if SameText(ProviderName, 'SQLOLEDB') then
                          s := ''
                      else if DataTypeCompatibility then
                          s := ' (with DataTypeCompatibility)'
                      else
                          s := ' (without DataTypeCompatibility)';
                  
                      cmd.Set_ActiveConnection(cn);
                      try
                          cmd.Execute({out}recordsAffected, EmptyParam, adExecuteNoRecords);
                          WriteLn('Provider '+ProviderName+s+': success.');
                      except
                          on E:Exception do
                              begin
                                  WriteLn('Provider '+ProviderName+s+' failed: '+E.Message);
                              end;
                      end;
                  
                  end;
                  
                  procedure Main;
                  begin
                      CoInitialize(nil);
                  
                      Test('SQLOLEDB', False);        //SQL Server client that ships with Windows since 2000
                  
                      Test('SQLNCLI', False);     //SQL Server 2005 native client
                      Test('SQLNCLI', True);      //SQL Server 2005 native client, w/ DataTypeCompatibilty
                  
                      Test('SQLNCLI10', False);   //SQL Server 2008 native client
                      Test('SQLNCLI10', True);    //SQL Server 2008 native client, w/ DataTypeCompatibilty
                  
                      Test('SQLNCLI11', False);   //SQL Server 2012 native client
                      Test('SQLNCLI11', True);    //SQL Server 2012 native client, w/ DataTypeCompatibilty
                  
                      Test('MSOLEDBSQL', False);  //SQL Server 2016 native client
                      Test('MSOLEDBSQL', True);   //SQL Server 2016 native client, w/ DataTypeCompatibilty
                  end;
                  
                  
                  begin
                    try
                      Main;
                    except
                      on E: Exception do
                        Writeln(E.ClassName, ': ', E.Message);
                    end;
                      WriteLn('Press enter to close');
                      ReadLn;
                  end.
                  

                  And while this is not a Delphi-specific question; I am using Delphi. So it's tagged as Delphi. If you complain I'm going to choke your tongue out.

                  Note: This is not ADO.net, it is ADO. It is not managed .NET Framework Class Library, it is the native Win32 COM OLE DB API.

                  解決方案

                  BrakNicku had the answer.

                  Set NumericScale property of your parameter to anything in 1-7 range.

                  Changing the code from:

                  Parameter p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                  

                  to

                  Parameter p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                  p.NumericScale = 1;
                  

                  works.

                  It even works with SQLOLEDB driver against SQL Server 2000.

                  Precision and Scale of different data types

                  Returning rowsets from SQL Server containing different data types, i can ask OLEDB what the Precision, and NumericScale of various T-SQL data types are:

                  SQL Server type   ADO type               Precision  NumericScale  DefinedSize
                  ----------------  ---------------------  ---------  ------------  -----------
                  int               adInteger (3)          10         255           4
                  real              adSingle (4)           7          255           4
                  money             adCurrency (6)         19         255           8
                  bit               adBoolean (11)         255        255           2
                  tinyint           adUnsignedTinyInt (17) 3          255           1
                  bigint            adBigInt (20)          19         255           8
                  uniqueidentifier  adGUID (72)            255        255           16
                  char(35)          adChar (129)           255        255           35
                  nchar(35)         adWChar (130)          255        255           35
                  decimal(15,5)     adNumeric (131)        15         5             19
                  datetime          adDBTimeStamp (135)    23         3             16
                  varchar(35)       adVarChar (200)        255        255           35
                  text              adLongVarChar (201)    255        255           2147483647
                  varchar(max)      adLongVarChar (201)    255        255           2147483647
                  nvarchar(35)      adVarWChar (202)       255        255           35
                  nvarchar(max)     adLongVarWChar (203)   255        255           1073741823
                  xml               adLongVarWChar (203)   255        255           1073741823
                  image             adLongVarBinary (205)  255        255           2147483647
                  varbinary(max)    adLongVarBinary (205)  255        255           2147483647
                  

                  Since SQL Server returns a datetime field with a NumericScale of 3; there may be a virtue in changing:

                  Parameter p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                  p.NumericScale = 1;
                  

                  to

                  Parameter p = cmd.CreateParameter("", adDBTimeStamp, adParamInput, 0, v);
                  p.NumericScale = 3;
                  

                  Bonus Reading

                  Don't ever, ever, EVER try to parameterize a datetime as adDBTimestamp. There are data-loss bugs in Microsoft's SQL Server OLEDB drivers (all of them):

                  • SQLOLEDB (1999) - Fails
                  • SQLNCLI (2005) - Fails
                  • SQLNCLI10 (2008) - Fails
                  • SQLNCLI11 (2010) - Fails
                  • MSOLEDBSQL (2012) - Fails

                  The correct answer is to parameterize all datetime values as a string (e.g. adVarChar) using the "ODBC 24-hour format":

                  • yyyy-mm-dd hh:mm:ss.zzz
                  • 2021-03-21 18:16:22.619

                  這篇關于當 DataTypeCompatility 打開時,如何將 12/30/1899 參數化為 SQL Server 本機客戶端?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不編寫 SQL 查詢的情況下找出數據庫列表和 SQL Server 實例使用的空間嗎?) - IT屋-程序員軟件開發
                  How to create a login to a SQL Server instance?(如何創建對 SQL Server 實例的登錄?)
                  How to know the version and edition of SQL Server through registry search(如何通過注冊表搜索知道SQL Server的版本和版本)
                  Why do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(為什么會出現“數據類型轉換錯誤?使用 ExecuteNonQuery()?)
                  How to show an image from a DataGridView to a PictureBox?(如何將 DataGridView 中的圖像顯示到 PictureBox?)
                  WinForms application design - moving documents from SQL Server to file storage(WinForms 應用程序設計——將文檔從 SQL Server 移動到文件存儲)

                  <small id='pORg3'></small><noframes id='pORg3'>

                  1. <legend id='pORg3'><style id='pORg3'><dir id='pORg3'><q id='pORg3'></q></dir></style></legend>
                      <i id='pORg3'><tr id='pORg3'><dt id='pORg3'><q id='pORg3'><span id='pORg3'><b id='pORg3'><form id='pORg3'><ins id='pORg3'></ins><ul id='pORg3'></ul><sub id='pORg3'></sub></form><legend id='pORg3'></legend><bdo id='pORg3'><pre id='pORg3'><center id='pORg3'></center></pre></bdo></b><th id='pORg3'></th></span></q></dt></tr></i><div class="l5f7rbx" id='pORg3'><tfoot id='pORg3'></tfoot><dl id='pORg3'><fieldset id='pORg3'></fieldset></dl></div>

                        <tbody id='pORg3'></tbody>

                        • <bdo id='pORg3'></bdo><ul id='pORg3'></ul>

                            <tfoot id='pORg3'></tfoot>

                          1. 主站蜘蛛池模板: 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 中矗模型-深圳中矗模型设计有限公司| 活性炭-果壳木质煤质柱状粉状蜂窝活性炭厂家价格多少钱 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 温室大棚建设|水肥一体化|物联网系统 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 连续密炼机_双转子连续密炼机_连续式密炼机-南京永睿机械制造有限公司 | 同学聚会纪念册制作_毕业相册制作-成都顺时针宣传画册设计公司 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 定制液氮罐_小型气相液氮罐_自增压液氮罐_班德液氮罐厂家 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 润滑油加盟_润滑油厂家_润滑油品牌-深圳市沃丹润滑科技有限公司 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | ALC墙板_ALC轻质隔墙板_隔音防火墙板_轻质隔墙材料-湖北博悦佳 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 水冷式工业冷水机组_风冷式工业冷水机_水冷螺杆冷冻机组-深圳市普威机械设备有限公司 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 深圳市超时尚职业培训学校,培训:月嫂,育婴,养老,家政;化妆,美容,美发,美甲. | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 湖南教师资格网-湖南教师资格证考试网 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | ★济南领跃标识制作公司★济南标识制作,标牌制作,山东标识制作,济南标牌厂 |