.NET Framework类型映射到DBTYPE,怪异的字符串,为nvarchar和焦炭 [英] mapping of .net framework types to dbtype, the weird string, nvarchar and char

查看:717
本文介绍了.NET Framework类型映射到DBTYPE,怪异的字符串,为nvarchar和焦炭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现约串映射(.NET Framework类型)和相应的数据类型为nvarchar(DBTYPE)上的 MSDN ,它说:

i found informations about mapping of string(.net framework type) and corresponding nvarchar(dbtype) on msdn, which says:

这样的隐式转换将失败,如果字符串比一个nvarchar,这是4000个字符的最大尺寸。对于字符串   大于4000个字符,明确设置的SqlDbType。

'This implicit conversion will fail if the string is larger than the maximum size of an NVarChar, which is 4000 characters. For strings larger than 4000 characters, explicitly set the SqlDbType.'

我只是不能figurt出什么`意思的明确设置。所以我写了一些code如下:

i just can not figurt out what`s mean by the "explicitly set". so i write some code as following:

char[] c = new char[5000];
for (int i = 0; i < 5000; i++)
{
    c[i] = 'a';
}
string s = new string(c);
using (SqlConnection conn = new SqlConnection(connstr))
{ 
    conn.Open();
    // create command object
    var comm = conn.CreateCommand();
    comm.CommandText = "select @s";

    // create parameter for command
    var p = comm.CreateParameter();
    p.ParameterName = "@s";
    // p.DbType = DbType.String;
    // p.Size = 5000;
    p.Value = s;

    // add parameter to command
    comm.Parameters.Add(p);

    // execute command
    var r = comm.ExecuteScalar();
}

正如你看到的,我没有不设置参数的类型和参数的大小,因此根据MSDN上的说明,我以为会有运行时异常。但不幸的是,它运行完全正确的,结果r为一个字符串包含5000'A'。 其实,不管我发表评论,或者取消设置parameter`s类型,规模的code,结果R永远是正确的,始终是相同的。

as you see, i didn not set parameter type and parameter size, so based on the instructions on msdn, i thought there would be an exception during runtime. but unfortunately, it runs perfectly right, the result r was a string contains 5000 'a'. in fact, no matter i comment or uncomment the code of setting parameter`s type, size, result r will always be right, always be the same.

plz帮助我,是我误会了一些有关指令的MSDN?

plz help me, am i misunderstood something about the instruction on msdn?

感谢你。

推荐答案

我不能完全确定你的问题是什么。你糊涂了,因为文件说,有一件事;但你可以做点别的?

I'm not completely sure what your question is. Are you confused, because the documentation says one thing; yet you are able to do something else?

您明确地使用下面的设置参数类型...

You explicitly set the parameter type by using the following...

var p = comm.CreateParameter();
p.SqlDbType = System.Data.SqlDbType.VarChar;
p.Size = 5000;
p.ParameterName = "@s";
p.Value = 'c';

编辑:根据您提供的使用说明书,一路上我跨preT它的尺寸可以比4000更大;你只需要明确设置的SqlDbType。在过去,当我遇到这种情况,行为要么会截断数据,或者它会给我一个errror指出字符串或二进制数据将被截断。

based on the documentation you included, the way I interpret it, the size can be larger than 4000; you just have to set the SqlDbType explicitly. In the past, when I've encountered this, the behavior was either it would truncate the data, or it would give me an errror stating that string or binary data would be truncated.

不幸的是,SQLServer是不是我的最强技能。

Unfortunately, SqlServer is not my strongest skill set.

这篇关于.NET Framework类型映射到DBTYPE,怪异的字符串,为nvarchar和焦炭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆