RE: ADO.NET 2.0 saving single space to SQL?



It turns out that changing the parameter to a type of varchar or using the
method signature that only specifies the colunm name and the value avoids
this problem. It seems likely that it is a bug in in the implicit conversion
from string to char to varchar. I think it should either work like 1.1 or it
should throw an exception.

"Donald Joppa" wrote:

I've created a much simpler test that demonstrates this on my machine when
compiled with the .NET 2.0 framework.

Here's the C# code:

public static void InsertRecord()
{
SqlConnection conn = new
SqlConnection("Server=.;Database=SqlDataAdapterTest;Trusted_Connection=Yes;");
SqlCommand cmd = new SqlCommand("AddRecord", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@TextField", SqlDbType.Char);
param.Value = string.Empty;
cmd.Parameters.Add(param);

try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (conn != null)
conn.Close();
}
}

Here's the simple stored procedure:

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO


CREATE PROCEDURE dbo.AddRecord

@TextField varchar(50) = null

as

Insert into table1
(TextField)
values (@TextField)


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Here's the table definitiion:

CREATE TABLE [Table1] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TextField] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

Here's the query that shows that the length of the column is 1 and the space:

select datalength(textfield), '[' + textfield + ']' from table1


"Donald Joppa" wrote:

We're converting from framework 1.1 to 2.0 and something interesting has
popped up. Fields that are being inserted into the database as a zero length
string in 1.1 contain a single space when inserted with 2.0 compiled code.

The code specifies a stored proc to run and set up the parameter collection.
I've verified in the debugger that in the parameter collection's item array
entries that I care about contain a zero length string (this is true for both
1.1 and 2.0).

SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
database compatibility level and it is set to 80.

When I capture the insert command via SQL profiler from 1.1 the parameters
show up as a zero length string (''), but when it comes from 2.0 code the
same parameters contain a single space (' ').

Anyone have any idea what I'm missing?
.



Relevant Pages

  • REPRESENT revisited
    ... Forth-94 has few floating point display functions [none in the ... If u is greater than zero the character string shall consist ... If u is zero the string shall consist of one digit representing ...
    (comp.lang.forth)
  • Re: Reproducing java calculations in UNIX
    ... quotient goes in x, also a double. ... is greater than zero) only the fractional part of the ... {fraction is 52 zero bits, ... is converted from string to floating point (string ...
    (comp.unix.shell)
  • Re: REPRESENT revisited
    ... >has given rise to implementations that differ in critical ... The required compatibility is 1), but 2) makes it much easier to get ... > If u is zero the string shall consist of one digit representing ... But I think it is a very bad idea if a word that should fill a string ...
    (comp.lang.forth)
  • Re: D3 Accented Characters and Code Pages
    ... The LCMapString function either maps an input character string to ... Specifies a locale identifier. ... the string mapping or sort key generation. ... LCMAP_HALFWIDTH Uses narrow characters. ...
    (comp.databases.pick)
  • Re: Allow Zero Length String Property - 2000 vs 2003
    ... Are you the same person who posted the 'Allow Zero Length String in MS ... "Ash" wrote in message ...
    (microsoft.public.access.tablesdbdesign)

Quantcast