nVarchar和SqlParameter [英] nVarchar and SqlParameter

查看:159
本文介绍了nVarchar和SqlParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发必须支持多种语言的应用程序.为了解决特殊字符问题,我在文本字段中使用了NVarhcar.因此,我对文本字段的SQL查询是

I'm developing an application which must support several languages. To solve the special characters problem I'm using NVarhcar for my text fields. So my SQL query for a text field is

insert into tbl_text(text)values(N'Chci tančit v oblasti')

我的问题是将其放在SqlCommand中,其中"insert into tbl_text(text)values(N@text)".当然,它会将"N@text"保存在数据库表中.

My problem is to put it in SqlCommand, wich is "insert into tbl_text(text)values(N@text)". It saves "N@text" in the DB table, sure.

你们知道怎么做吗?我正在使用C#和SQL 2008.

Do you guys know someway to do it? I'm using C# and SQL 2008.

抱歉,如果很难理解我的问题.我的英语不好=/

Sorry if it was hard to understand my question. My English is poor =/

推荐答案

Add(string,object)出于这个原因已被弃用(来自SQL Server团队的Pablo Castro):

Add(string, object) has been deprecated for this reason (from Pablo Castro of the SQL Server team):

问题在于C#和 VB.NET编译器将公开 此代码的怪异行为:

The problem is that both the C# and the VB.NET compilers will expose very weird behavior for this code:

command.Parameters.Add("@ p",0);

command.Parameters.Add("@p", 0);

您可能希望它使用 带有对象的重载, 给它赋值0,但是 它会选择需要一个 SqlDbType作为第二个参数!在 为了避免这种情况(并可能 其他)之间的歧义 添加(字符串,sqldbtype)和添加(字符串, 对象),我们已弃用Add(string, 对象)并介绍 AddWithValue(字符串,对象).在 一般,有多个重载 区别参数在哪里 类型是对象",其中之一是 危险的事情.

you may expect this to use the overload that takes an object and assign the value 0 to it, but instead it will pick the overload that takes a SqlDbType as the second parameter! In order to avoid this (and potentially others) ambiguity between the Add(string, sqldbtype) and Add(string, object), we deprecated Add(string, object) and introduced AddWithValue(string, object). In general, having multiple overloads where the distinguishing parameter type is "object" in one of them is a dangerous thing to do.

这篇关于nVarchar和SqlParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆