Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I create an OdbcCommand and add parameters, parameters that are of VarChar 4000 are translated into text type in generated sql. My sample code is:

C#
OdbcConnection dbConnection = new OdbcConnection("DSN=database;UID=uid;PWD=pwd;");
try
{
    dbConnection.Open();
    OdbcCommand odbcCommand = new OdbcCommand();
    odbcCommand.CommandText = "UPDATE Table SET Notes = ? WHERE Notes = ?";
    odbcCommand.Connection = dbConnection;
    odbcCommand.Parameters.Add("", OdbcType.VarChar, 3999, "");
    odbcCommand.Parameters.Add("", OdbcType.VarChar, 4000, "");
    odbcCommand.Parameters[0].Value = "0";
    odbcCommand.Parameters[1].Value = "1";
    odbcCommand.ExecuteNonQuery();
}
finally
{
    dbConnection.Close();
}


This is the generated SQL, that I got from SQL Server Profiler

SQL
exec sp_executesql N'UPDATE Table SET Notes = @P1 WHERE Notes = @P2',N'@P1 varchar(3999),@P2 text','0','1'


Even though MSDN does not say anything about maximum sizes for VarChar here. Does anyone know why this happens and if this is configurable?

I believe it's a OdbcCommand specific thing because the update SQL statement that Simple Data generates for other field that is stored as varchar(4000) in the SQL Server database is generated correctly.
Posted

1 solution

The solution was to switch to 3999 size field instead of 4000. This satisfies requirements and purpose of the change
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900