查看: 1655|回复: 6
|
How to insert chinese characters into a table in SQL server
[复制链接]
|
|
i hv a question regarding sql, hope anyone of u can help..
does anyone knows how to insert chinese characters into a table in sql server?
i hv try this:
declare @text as nvarchar(999)
set @text=N'谢谢'
insert into tableChinese(chinese) values (@text)
It works.. but when i want to pass chinese characters from an application, i dont know how to add the 'N' together with the @text at the values part ..something like this:
insert into tableChinese(chinese) values (N @text)
"values (N @text)" is not correct..
thanks |
|
|
|
|
|
|
|
发表于 28-3-2006 12:18 PM
|
显示全部楼层
你是在用 stored procedured 吗?
如果不是的话, 你其实不用 declare 啊.
如果在 c#, 你可以直接这样写:
sql = "insert into tableChinese(chinese) values (N" + chinese_text + ")" |
|
|
|
|
|
|
|
楼主 |
发表于 28-3-2006 12:26 PM
|
显示全部楼层
yes, im using stored procedure.. and VB.net
something like this:
CREATE PROCEDURE dbo.ChineseCharIns
(
@chinese as nvarchar(999)
)
as
declare @chineseCharID as bigint
SET XACT_ABORT ON
begin transaction
begin
Insert into ChineseChar(chinese)
values (@chinese)
Select @chineseCharID =@@identity
commit transaction
GO
So, i hv to add "N" infront of the @chinese to make sure i can insert the chinese characters into table.. if not, symbols like "???" will be inserted.. |
|
|
|
|
|
|
|
发表于 28-3-2006 01:50 PM
|
显示全部楼层
原来如此, 我的做法是, 保持你以上的 Stored Procedure 的不变.
但, 我在 VB.NET/C# 里执行这 stored procedures 时, 我会这样呼叫:
sql = "ChineseCharIns N'" & chinese_characters & "'"
p/s: stored procedure 里不需要加 "N". |
|
|
|
|
|
|
|
楼主 |
发表于 28-3-2006 02:00 PM
|
显示全部楼层
如果我执行 stored procedure 的方法如下又怎样把那个"N"加入呢?
Dim cnn As New SqlConnection(Application("sqlCnn"))
Dim dcd As New SqlCommand
Dim prm As SqlParameter
Try
'init
dcd.Connection = cnn
dcd.CommandText = "ChineseCharIns"
dcd.CommandType = CommandType.StoredProcedure
'declare parameter
prm = New SqlParameter("@chinese", SqlDbType.NVarChar, 999)
dcd.Parameters.Add(prm)
'assign parameter value
dcd.Parameters("@chinese").Value = txtChineseChar.Text
'open connection and execution
cnn.Open()
dcd.ExecuteNonQuery()
'close connection
cnn.Close()
cnn = Nothing
Catch except As SqlException
Throw except
Catch ex As Exception
Throw ex
Finally
cnn = Nothing
End Try |
|
|
|
|
|
|
|
楼主 |
发表于 28-3-2006 02:11 PM
|
显示全部楼层
i think i hv got the correct answer..thanks anyway |
|
|
|
|
|
|
|
楼主 |
发表于 28-3-2006 02:14 PM
|
显示全部楼层
actually with this.. no need to add "N" before the variable.. |
|
|
|
|
|
|
| |
本周最热论坛帖子
|