We have a strange problem using SqlCommand
to insert data into a SQL Server database. If we try to insert a large text into the column konnotiz1
, the text is being truncated after 43245 characters. For some reason I don't understand the real application code creates 42909 characters.
Here's a screenshot of the column properties page:
The following code causes the problem:
static void Test()
{
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(getConString()))
{
con.Open();
System.Data.SqlClient.SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "Insert into kontakte (konlfdnr, konNotiz1) values (@konlfdnr, @konNotiz1)";
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 60;
// you can ignore this param, it's owner pk-column.
System.Data.SqlClient.SqlParameter param = cmd.Parameters.Add("@konlfdnr", SqlDbType.Char, 10);
// better don't ask why we have to pad this... ;)
param.Value = "1".PadLeft(10);
param.Direction = ParameterDirection.Input;
param = cmd.Parameters.Add("@konNotiz1", SqlDbType.Text);
param.Direction = ParameterDirection.Input;
param.Value = getParamValue();
cmd.ExecuteNonQuery();
con.Close();
}
}
private static string getParamValue()
{
// my Textfile has something about 300000 characters. It's a html code from a mail.
return System.IO.File.ReadAllText("C:\\Temp\\insert.txt");
}
private static string getConString()
{
return @"Data Source=NB-JH1\SQLEXPRESS;Initial Catalog=Testsystem_Local;Integrated Security=True;Encrypt=True;TrustServerCertificate=True;Pooling=True;MultipleActiveResultSets=True;
}
Has anyone an idea, why the text is being truncated and how we can get this working?