Quantcast
Viewing latest article 7
Browse Latest Browse All 7

mapping of .net framework types to dbtype, the weird string, nvarchar and char

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

'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.'

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.Value = s;

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

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

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, these code works just fine. no warn, no failure, no exception.

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

thanks in advance.


Viewing latest article 7
Browse Latest Browse All 7

Trending Articles