user9204293
user9204293

Reputation:

How to insert Persian words into a SQL Server database?

I am just wondering how to insert Persian characters into my service-based database?

When I save my data it shows something like '???'.

I have checked such questions like this. But, the solutions were not useful.

    private void button1_Click(object sender, EventArgs e)
    {            
            objConnection.Open();

            if (ctypeCheckBox.Checked == true)                
                st = 1;          
            else if (ctypeCheckBox.Checked == false)                
                st = 0;

            string query = "INSERT INTO LectureTable(Cname, Cid, Ccredit, Csession, Ctype, CstartDate, CendDate, CstartTime, CendTime) VALUES('" + cnameTextBox.Text + "','" + cidTextBox.Text + "','" + ccreditTextBox.Text + "','" + csessionTextBox.Text + "','" + st + "', '" + cstartDateDateTimePicker.MinDate + "', '" + cendDateDateTimePicker.MaxDate + "', '" + cStartTimeBox.Text + "', '" + cEndTimeBox.Text + "')";
            SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
            SDA.SelectCommand.ExecuteNonQuery();
            MessageBox.Show("Inserted!");

            objConnection.Close();
    }

Upvotes: 1

Views: 1419

Answers (2)

Yuri
Yuri

Reputation: 2900

Two things:

  1. Never ever combine your query string with values

    "INSERT INTO LectureTable(Cname, Cid, Ccredit, Csession, Ctype, CstartDate, CendDate, CstartTime, CendTime) VALUES('" + cnameTextBox.Text + "','" + cidTextBox.Text + "','" + ccreditTextBox.Text + "','" + csessionTextBox.Text + "','" + st + "', '" + cstartDateDateTimePicker.MinDate + "', '" + cendDateDateTimePicker.MaxDate + "', '" + cStartTimeBox.Text + "', '" + cEndTimeBox.Text + "')";
    

    Should be immediately replaced with

    "INSERT INTO LectureTable(Cname, Cid, Ccredit, Csession, Ctype, CstartDate, CendDate, CstartTime, CendTime) 
    VALUES(@cname, @cid, @ccredit, @csession, @st, @cstartDateDate, @cendDate, @cStartTime, @cEndTimeB)";
    

and then you should use

SDA.SelectCommand.Parameters.AddWithValue("cname",cnameTextBox.Text);

for all parameters. This will save you from a lot of problems including SQL injection.

  1. In the database your columns should have nvarchar data type.

Good luck

Upvotes: 6

Ehsan Ullah Nazir
Ehsan Ullah Nazir

Reputation: 1917

You should use SqlParameter .Giving example of only one parameter.You can add others as same way.

 string query = "INSERT INTO LectureTable(Cname) VALUES(@name)";          

using(SqlCommand cmd = new SqlCommand(query, SqlConnection))
{ 

SqlParameter param = new SqlParameter("@name", cnameTextBox.Text);
param.SqlDbType = SqlDbType.String;
cmd.Parameters.Add(param);
.....
}

Upvotes: 0

Related Questions