Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-非拉丁字符在数据库Visual Studio中显示不正确_C#_Asp.net - Fatal编程技术网

C#-非拉丁字符在数据库Visual Studio中显示不正确

C#-非拉丁字符在数据库Visual Studio中显示不正确,c#,asp.net,C#,Asp.net,下面是我的代码,它将动态gridview的文本框中的元素插入数据库。代码工作得很好,但我的问题是,以非拉丁字符(希腊字符)键入的插入元素在数据库中不正确显示,而是如下所示: 请告诉我如何正确插入希腊字符?代码如下: private void InsertRecords(StringCollection sc) { StringBuilder sb = new StringBuilder(string.Empty); string[] splitItems

下面是我的代码,它将动态gridview的文本框中的元素插入数据库。代码工作得很好,但我的问题是,以非拉丁字符(希腊字符)键入的插入元素在数据库中不正确显示,而是如下所示:

请告诉我如何正确插入希腊字符?代码如下:

private void InsertRecords(StringCollection sc)
    {
        StringBuilder sb = new StringBuilder(string.Empty);
        string[] splitItems = null;
        const string sqlStatement = "INSERT INTO P_interventions (Date,P_Id,Simeio,Aitio,Etos,Therap) VALUES";
        int id = Convert.ToInt32(Session["pa_id"]);
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
                sb.AppendFormat("{0}(@Date, @p_id ,'{1}','{2}','{3}','{4}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3]);
            }
        }

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            connection.Open();
            using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
            {
                cmd.Parameters.AddWithValue("@p_id", id);
                cmd.Parameters.AddWithValue("@Date", DateTime.Now.ToShortDateString());
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
        }
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = "The records have benn inserted successfuly!";
    }

 protected void BtnSave_Click(object sender, EventArgs e)
    {
        //εγχειρήσεις
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values  
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("DropDownList2");
                    //get the values from TextBox and DropDownList  
                    //then add it to the collections with a comma "," as the delimited values  
                    sc.Add(string.Format("{0},{1},{2},{3}", box1.Text, box2.Text, box3.Text, ddl2.SelectedItem.Text));
                    rowIndex++;
                }
                //Call the method for executing inserts  
                InsertRecords(sc);
            }
        }
private void InsertRecords(StringCollection sc)
{
StringBuilder sb=新的StringBuilder(string.Empty);
字符串[]splitItems=null;
const string sqlStatement=“插入P_干预(日期、P_Id、Simeio、Aitio、Etos、治疗)值”;
int id=Convert.ToInt32(会话[“pa_id]”);
foreach(sc中的字符串项)
{
如果(项包含(“,”))
{
splitItems=item.Split(“,”.ToCharArray());
sb.AppendFormat(“{0}(@Date,@p_id,{1},{2},{3},{4}”);”,sqlStatement,splitItems[0],splitItems[1],splitItems[2],splitItems[3]);
}
}
使用(SqlConnection=newsqlconnection(GetConnectionString()))
{
connection.Open();
使用(SqlCommand cmd=newsqlcommand(sb.ToString(),connection))
{
cmd.Parameters.AddWithValue(“@p_id”,id);
cmd.Parameters.AddWithValue(“@Date”,DateTime.Now.ToShortDateString());
cmd.CommandType=CommandType.Text;
cmd.ExecuteNonQuery();
}
}
lblMessage.ForeColor=System.Drawing.Color.Green;
lblMessage.Text=“记录已成功插入!”;
}
受保护的无效BtnSave\u单击(对象发送方,事件参数e)
{
//εγχειρήσεις
int rowIndex=0;
StringCollection sc=新的StringCollection();
如果(ViewState[“CurrentTable”]!=null)
{
DataTable dtCurrentTable=(DataTable)视图状态[“CurrentTable”];
如果(dtCurrentTable.Rows.Count>0)
{

对于(int i=1;i,由于您在数据库中使用了nchar字段(如注释中所述),您的数据库已经支持Unicode字符串。但是,您正在传递要插入的值,如SQL中所述:

'...'
您需要将它们作为Unicode字符串传递:

N'...'

现在不要只在字符串文本前面加一个
N
还有一件事你做错了:你通过字符串连接传递用户提供的值,这是一个严重的安全性和稳定性问题。改用参数-你已经知道如何使用参数,因为你是为
@p\u id这样做的
@Date
。对字符串值执行相同的操作。这也将解决Unicode问题,因为字符串默认为Unicode参数类型。

我认为您必须在数据库中使用Unicode数据类型,而不是常规数据类型(例如:使用NVarchar代替Varchar)

同样,在代码中,在字符串字段前面使用N,如

 sb.AppendFormat("{0}(@Date, @p_id ,N'{1}',N'{2}',N'{3}',N'{4}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3]);

“{0}(@Date、@p_id、{1}、{2}、{3}、{4}”)
是错误的。您也应该通过参数传递最后四个元素。您没有将它们作为参数传递。您是将它们作为varchar字符串文本传递。您没有
@xxx
cmd.parameters。为它们添加
。以及使用不正确的数据类型,您的方法对SQL注入开放。因为您将声明将参数设置为nvarchar而不是varchar。它还解决了SQL注入的另一个问题,而您当时并不知道这一问题。在2015年,没有理由编写这种易于SQL注入的代码。我希望您接受它,不要只是在那里加上
N
前缀。否则,如果您的站点处于t状态,它肯定会遭到黑客攻击他从众多自动爬虫中寻找这些漏洞的其中一个爬虫那里找到了互联网。@PankajGupta停止教坏习惯。谢谢你的回答!谢谢你的回答!