When calling a stored procedure with ADO.NET, how do I tell what parameter types the database actually used?

huangapple go评论51阅读模式
英文:

When calling a stored procedure with ADO.NET, how do I tell what parameter types the database actually used?

问题

我在C#中有一个调用存储过程的代码。它使用ADO.NET调用位于我的SQL Server上的存储过程。尽管我知道服务器上存储过程的一个参数是 VARCHAR(120),但我在C#中使用 AddWithValue 将其添加到参数集合中,得出结论是在C#中使用的字符串参数应该转换为 SqlDbTypeNVARCHAR。我该如何判断服务器是否使用了 NVARCHAR 类型还是 VARCHAR 类型?关于ADO.NET是否足够智能地从服务器读取正确的参数类型,我得到了一些混乱的信息,我想通过实证测试来验证。

我的当前简陋解决方案如下:

  1. 选取一个在 VARCHAR 中不存在的字符,例如 ǹ 即 'Latin Small Letter N With Grave'。
  2. 观察 SQL Server 将 SELECT ǹ 转换为 ?
  3. 编写一个查询,在输入 ? 时返回值。
  4. 将该查询输入 ǹ
  5. 观察查询的行为,好像我输入的是 ?
  6. 得出结论,即使我给它 NVARCHAR,SQL 仍然使用了 VARCHAR
英文:

I have a stored procedure call in C#. It uses ADO.NET to call the stored procedure that is on my SQL Server. Despite knowing that one of the parameters of the stored procedure on the server is VARCHAR(120), I used AddWithValue to add it to my parameter collection in C# and that has concluded that the string parameter used in C# should be translated to a SqlDbType of NVARCHAR. How do I tell if the server has used the NVARCHAR type or the VARCHAR type? I'm getting mixed messages regarding if ADO.NET is smart enough to read the correct parameter type from the server and I would like to test this empirically.

My current crude solution was as follows:

  1. Take a character that does not exist in VARCHAR, e.g. ǹ i.e. 'Latin Small Letter N With Grave'.
  2. Observe that SQL Server translates SELECT ǹ to ?.
  3. Write a query that returns values when ? is fed to it.
  4. Feed said query ǹ.
  5. Observe that the query has acted as if I fed it ?.
  6. Conclude that SQL has used VARCHAR even though I gave it NVARCHAR.

答案1

得分: 1

以下是翻译好的部分:

"> that has concluded that the string parameter used in C# should be translated to a SqlDbType of NVARCHAR. How do I tell if the server has used the NVARCHAR type or the VARCHAR type? I'm getting mixed messages regarding if ADO.NET is smart enough to read the correct parameter type from the server and I would like to test this empirically.

There is no such functionality. ADO.Net does not query anything from the server, it just sends whatever you tell it. If that has defaulted to nvarchar then that is what will be sent. This is why you should always specify the data types and lengths/precision/scale.

Having said that, in the case of stored procedures, the server will then convert the data back to the original type to fit into the parameter declaration, with a slight overhead, and possible loss of precision or characters etc."

英文:

> that has concluded that the string parameter used in C# should be translated to a SqlDbType of NVARCHAR. How do I tell if the server has used the NVARCHAR type or the VARCHAR type? I'm getting mixed messages regarding if ADO.NET is smart enough to read the correct parameter type from the server and I would like to test this empirically.

There is no such functionality. ADO.Net does not query anything from the server, it just sends whatever you tell it. If that has defaulted to nvarchar then that is what will be sent. This is why you should always specify the data types and lengths/precision/scale.

Having said that, in the case of stored procedures, the server will then convert the data back to the original type to fit into the parameter declaration, with a slight overhead, and possible loss of precision or characters etc.

huangapple
  • 本文由 发表于 2023年5月14日 01:50:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定