How to insert Arabic characters into SQL database?
For the field to be able to store unicode characters, you have to use the type nvarchar
(or other similar like ntext
, nchar
).
To insert the unicode characters in the database you have to send the text as unicode by using a parameter type like nvarchar
/ SqlDbType.NVarChar
.
(For completeness: if you are creating SQL dynamically (against common advice), you put an N before a string literal to make it unicode. For example: insert into table (name) values (N'Pavan')
.)
Insert arabic words in sql server
Use unicode datatypes for your data storage and begin your strings with N literal like this:
declare @t table(col Nvarchar(100));
insert @t (col) values (N'your text');
Or use the appropriate COLLATION with non-unicode data types but pass your values as unicode ones (with N) because your server/database collation seems to be different from arabic
SQL Server : insert Arabic letter to database
IF you want to insert Unicode string literals, you must prepend your string literal with an N
prefix - try this:
SET @textEnglish = N'...(insert your Arabic text here)...'
Otherwise, your text is reverted back to a non-Unicode format before being stored - and that's why you lose the Arabic text....
And also: if you're concatenating with VARCHAR
parameters, I'd recommend using an explicit CAST
to NVARCHAR
(include a length when casting!):
SET @textEnglish = N'Dear customer, your Card linked to account number ' + CAST(@vAccount AS NVARCHAR(100))
How to insert Arabic text via SQLCommand in SQL Server using C#
You need to set the collation for you database to be arabic, and also that of the table column.
Also make sure the column is NVARCHAR and not VARCHAR
How to insert unicode (arabic) characters into SQL Server database
You should always use parametrized queries to avoid SQL injection attacks. Parameters also give you the ability to explicitly define what data types and which length you want. Furthermore, by using parameters, you don't need to fiddle with lots of single and double quotes and so forth - the code becomes much cleaner and easier to read - and you avoid a lot of errors, too!
Try code something like this:
// define your INSERT statement with PARAMETERS
string insertStmt = "INSERT INTO dbo.Table1(title, datee, post, cat, imageurl) " +
"VALUES(@title, @datee, @post, @cat, @imageurl)";
// define connection and command
using(SqlConnection conn = new SqlConnection(yourConnectionStringHere))
using (SqlCommand cmd = new SqlCommand(insertStmt, conn))
{
// define parameters and set their values
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 100).Value = TextBox1.Text.Trim();
cmd.Parameters.Add("@datee", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@post", SqlDbType.NVarChar, 100).Value = TextBox2.Text.Trim();
cmd.Parameters.Add("@cat", SqlDbType.NVarChar, 100).Value = DropDownList1.SelectedItem.Text.Trim();
cmd.Parameters.Add("@imageurl", SqlDbType.NVarChar, 250).Value = path;
// open connection, execute query, close connection
conn.Open();
int rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
}
store arabic in SQL database
You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar)
CREATE TABLE #test
(
col1 VARCHAR(100) COLLATE Latin1_General_100_CI_AI,
col2 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
col3 NVARCHAR(100)
)
INSERT INTO #test VALUES(N'لا أتكلم العربية',N'لا أتكلم العربية',N'لا أتكلم العربية')
Note the N before values in insert statement above. If you do not mention it, system will treat the values as Varchar, not NVarchar.
SELECT * FROM #test
Returns
col1 col2 col3
------------------------------ ------------------------------ ------------------------------
?? ????? ??????? لا أتكلم العربية لا أتكلم العربية
To see a list of Arabic collations use
SELECT name, description
FROM fn_helpcollations()
WHERE name LIKE 'Arabic%'
Related Topics
How to Return a Incremental Group Number Per Group in SQL
SQL Query for Getting Data for Last 3 Months
How to Count in SQL All Fields with Null Values in One Record
Converting String List into Int List in SQL
Left Join Query Not Returning All Rows in First Table
How to Retrieve the Current Value of an Oracle Sequence Without Increment It
Restrict Varchar() Column to Specific Values
Varchar as Foreign Key/Primary Key in Database Good or Bad
How to Create an Oracle Sequence Starting with Max Value from a Table
Postgresql Join with Array Type with Array Elements Order, How to Implement
SQL Error: Ora-00942 Table or View Does Not Exist
Execute a Stored Procedure in Another Stored Procedure in SQL Server
How to Call an Oracle Stored Procedure from an Excel Vba Script
Find Last Row in Group by Query-SQL Server