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.DbType = DbType.String;
// p.Size = 5000;
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, result r will always be right, always be the same.
plz help me, am i misunderstood something about the instruction on msdn?
thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…