1

//如何使用 N 字符让我的程序更新阿拉伯语字段(名字="الاسم الاول")...我的意思是

UPDATE  students SET first_name = N'الاسم' , last_name = N'الاسم الاخير' , father_name = N'الاسم الاخير' , mother_name = '', birth_date = '1/1/1990 12:00:00 AM' , education_level = '' , address = '' , notes = '' WHERE student_id = 33

//此命令在 MSSQL-Server 中,但我试图在我的 C# 程序中使用 UPDATE 命令

 public string student(int id, string first_name, string last_name, string father_name, string mother_name, DateTime birth_date, string education_level, string address, 
         int[] phone_numbers, int[] old_phone_numbers, string notes)
    {
        SqlConnection SQLconnection = new SqlConnection(connectionString);
        SqlCommand command = SQLconnection.CreateCommand();
        SQLconnection.Open();
        command.CommandText = "UPDATE  students SET " +
        "first_name = " + "'" + first_name + "'" + " , last_name = " + "'" + last_name + "'" +
         " , father_name = " + "'" + father_name + "'" + " , mother_name = " + 
        "'" + mother_name + "'" + ", birth_date = " + "'" + birth_date + "'" +
        " , education_level = " + "'" + education_level + "'"  +
        " , address = " + "'" + address + "'" + " , notes = " + "'" + notes + "'" +
        " WHERE student_id = " + id ;
        command.ExecuteNonQuery();
        SQLconnection.Close();

//有没有比这些语法更好更清晰的方法?

4

1 回答 1

1

最好的方法是使用Parameters。像这样:

command.CommandText = @"UPDATE  students SET 
first_name = @first_name, 
last_name = @last_name,
//... and so on...
WHERE student_id = @id";
command.Parameters.AddWithValue("@first_name", first_name);
command.Parameters.AddWithValue("@last_name", last_name);
//... and so no...
command.Parameters.AddWithValue("@id", id);
于 2012-06-18T12:25:31.380 回答