企业库的DbCommand AddInParameter方法企业库、方法、DbCommand、AddInParameter

2023-09-06 19:16:36 作者:潇洒丶疯一回

我一直在使用企业库,因为很长一段时间。我们正在改变列varchar类型为nvarchar在数据库以支持不同的语言。我们确实有一个使用传统的方式连接到数据库,就像使用命令对象,一些旧的code。在这种code,创建sqlparameters时,指定参数的数据类型无论是int或VARCHAR。这很容易从varchar更改为nvarchar那里。

I have been using enterprise library since long time. We are changing column type varchar to nvarchar in database to support different language. We do have some legacy code that is using traditional way to connect to database like using command object. In that code, when creating sqlparameters, parameter's datatype is specified whether it is int or varchar. It's easy to change from varchar to nvarchar there.

不过,我想知道在使用企业库在这里我们指定的.NET数据类型像串并相信企业库调用存储过程时,其转换类型分为sqldatatype内部。

But I am wondering about while using enterprise library where we specify .net datatype like string and I believe enterprise library converts that type into sqldatatype internally when calling stored procedure.

db.AddInParameter(CMD@SortOrder,DbType.String,SortOrder的)

db.AddInParameter(cmd, "@SortOrder", DbType.String, SortOrder)

什么数据类型的企业库内部转换? VARCHAR或nvarchar?它是安全的nvarchar的使用遍布我们的地方遗留code?

What datatype enterprise library converts internally? varchar or nvarchar? Is it safe to use nvarchar all over the places in legacy code we have?

推荐答案

我假设你正在使用SQL Server。

I assume you are using SQL Server.

DbType.String为nvarchar的同时DbType.AnsiString为VARCHAR。 http://msdn.microsoft.com/en-us/library /system.data.dbtype.aspx

DbType.String is for nvarchar while DbType.AnsiString is for varchar. http://msdn.microsoft.com/en-us/library/system.data.dbtype.aspx

由于要调用存储过程,你的参数被自动转换成字符串的任何类型的存储过程中指定。例如,如果输入是 DbType.String但是参数为varchar,SQL Server会将它给你。不要做反向虽然(在code在DbType.AnsiString但存储过程预计为nvarchar),因为你只会得到ANSI的存储过程中的字符。

Since you are calling a stored procedure, your parameter gets automatically converted to whatever string type is specified in the stored procedure. For example if the input is DbType.String but the parameter is varchar, SQL Server converts it for you. Don't do it in reverse though (in the code in DbType.AnsiString but the stored procedure expects nvarchar) because you will only get the ansi chars in the stored procedure.

//Don't do this! you will get 'h?ello' in the SP instead of 'hܒello'
SqlParameter("input", "h\u0712ello") { DbType = System.Data.DbType.AnsiString });

另外,在EntLib,有一种方法,以调用存储过程,而无需指定参数名和数据类型。像这样:

Also, in EntLib, there is a way to call stored procedures without needing to specify the parameter names and data type. Like so:

//calls AddToInventory stored procedure
//the 2 parameters are automatically mapped.
db.ExecuteNonQuery("AddToInventory", 1, "Life Jacket");