2013-06-21 195 views
0

我试图在我的SQL Server数据库中插入字符串到char(20)列。如何将字符串转换为Char(20)sql数据类型

我设定的字符串为等于一个值,然后将其添加到每次我尝试它结束了在数据库

picture

这样看时间的参数

thisstring = "1914" 
cmd.Parameters.AddWithValue("@code", thisstring) 

然而

我需要它看起来像1913, 000000, 000001的。

当我尝试垫用空格

thisstring= thisstring.PadLeft(20, " ") 

thisstring = "  " & thisstring 

我越来越

字符串或二进制数据字符串将被截断

即使该字段不是总共20个字符

我在做什么错了?

编辑***这里是SQL Server

http://imgur.com/BbV6VQv

+0

什么是SQL或'cmd',即'cmd.CommandText'存储过程调用标准语法试试? – shahkalpesh

+0

thisstring = thisstring.PadLeft(20,“”)应该已经工作了。你可以仔细检查参数定义。这可能与表格定义不同。 –

+0

cmd =新的SqlCommand(strSQL,conn)和执行是cmd.ExecuteNonQuery() – user867621

回答

1

我没有绝对的把握之列,但我认为问题出在AddWithValue。
虽然方便,但此方法不允许指定传递给数据库引擎的确切数据类型,也不允许指定参数的大小。它总是传递一个C#UNICODE字符串的nvarchar参数。

我想你应该用来建立一个参数

Dim p = new SqlParameter("@code", SqlDbType.Char, 20) 
p.Value = thisstring.PadLeft(20, " ") 

See this very interesting article on MSDN

相关问题