C# ASP.NET (VS 2008 Exp) Database saves hebrew val

2019-09-14 16:06发布

As I haven't found a solution to my previous question (Link), I am posting a new one, hoping for a solution. I'm sorry if I am breaking the rules, but I haven't received a solution.

when I try to update a row, and I have tracked what query is sent, for example:

UPDATE global_status SET title = 'Login_Failure', info = 'שדכ' WHERE id = '2'

So you can see that the final query which is sent is in Hebrew, so it's not a problem before that. I execute it this way:

string Status_Update = "UPDATE global_status SET title = '" + Title + "', info = '" + Info + "' WHERE id = '" + Request.Form["Status_Key"] + "'";

MyAdoHelper.DoQuery(GlobalVar.DatabaseName, Status_Update);

The method:

public static void DoQuery(string fileName, string sql)
{
    SqlConnection conn = ConnectToDb(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    com.ExecuteNonQuery();
    com.Dispose();
    conn.Close();
}

After the update, the database contains ??? where I inserted Hebrew letters.

I can manually insert Hebrew values into the database, and also get them back.

I have another form (registration) where I am able to insert Hebrew values, using the DoQuery();

Here is the source code for the update page (it's updating a title and info of a record, nothing special):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_EditGlobalStatus : System.Web.UI.Page
{
    public string StatusesTable;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["Status_Update"] != null)
        {
            string Title = Request.Form["Status_Title"].Secure();
            string Info = Request.Form["Status_Info"].Secure();

            Info = Info.Replace("/s", "<span class\"res\">").Replace("/e", "</span>");

            string Status_Update = "UPDATE global_status SET title = '" + Title + "', info = '" + Info + "' WHERE id = '" + Request.Form["Status_Key"] + "'";

            MyAdoHelper.DoQuery(GlobalVar.DatabaseName, Status_Update);
        }

        if (Request.Form["Status_Delete"] != null)
        {
            string Status_Delete = "DELETE FROM global_status WHERE id = '" + Request.Form["Status_Key"] + "'";
            MyAdoHelper.DoQuery(GlobalVar.DatabaseName, Status_Delete);
        }

        string GetGlobalStatuses = "SELECT * FROM global_status";
        DataTable dt = MyAdoHelper.ExecuteDataTable(GlobalVar.DatabaseName, GetGlobalStatuses);

        if (dt.Rows.Count > 0)
        {
            StatusesTable = "<table cellspacing=\"15\">";

            StatusesTable += "<th>כותרת ההודעה</th><th>מידע ההודעה</th>";

            foreach (DataRow status in dt.Rows)
            {
                StatusesTable += "<tr><form method=\"post\" action=\"\">";

                StatusesTable += "<input type=\"hidden\" name=\"Status_Key\" value=\"" + status["id"].ToString() + "\" />";
                StatusesTable += "<td><input size=\"25\" dir=\"ltr\" type=\"text\" name=\"Status_Title\" value=\"" + status["title"].ToString() + "\" /></td>";
                StatusesTable += "<td><input size=\"90\" type=\"text\" name=\"Status_Info\" value=\"" + status["info"].ToString().Secure() + "\" /></td>";

                StatusesTable += "<td><input type=\"submit\" name=\"Status_Update\" value=\"עדכן\"><input type=\"submit\" name=\"Status_Delete\" value=\"מחק\"></td></form></tr>\r\n";
            }

            StatusesTable += "</table>";
        }
        else
        {
            StatusesTable = "<h1>אין משתמשים קיימים</h1>";
        }
    }
}

Hoping for a solution, Thanks!

Guy

1条回答
老娘就宠你
2楼-- · 2019-09-14 16:24

After reading several text explanation I wound that many people are capable to solve the problem with N at the beginning of string like this

 string Status_Update = "UPDATE global_status SET  title = '" +
 Title + "', info =N'" + Info + "'  WHERE id = '" +
 Request.Form["Status_Key"] + "'";

also make sure to try nvarchar or unicode

查看更多
登录 后发表回答