Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/88.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# 在AS400表中插入一个字符串,该字符串包含来自C的英语和希腊语字符的混合#_C#_Sql_Winforms_Unicode_Ibm Midrange - Fatal编程技术网

C# 在AS400表中插入一个字符串,该字符串包含来自C的英语和希腊语字符的混合#

C# 在AS400表中插入一个字符串,该字符串包含来自C的英语和希腊语字符的混合#,c#,sql,winforms,unicode,ibm-midrange,C#,Sql,Winforms,Unicode,Ibm Midrange,我试图在AS400表中插入一条新记录,其字符串值为希腊和英语字符的混合值 表格结构: CREATE TABLE RAVONLIBT.LAMBDA ( CHRCCS875 CHAR(5) CCSID 875 DEFAULT NULL , VARCCS875 VARCHAR(5) CCSID 875 DEFAULT NULL ) private void Retrieve_Click(object sender, EventArgs e) { var sql =

我试图在AS400表中插入一条新记录,其字符串值为希腊和英语字符的混合值

表格结构:

CREATE TABLE RAVONLIBT.LAMBDA ( 
CHRCCS875 CHAR(5) CCSID 875 DEFAULT NULL , 
VARCCS875 VARCHAR(5) CCSID 875 DEFAULT NULL )   
private void Retrieve_Click(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        sql.AppendFormat("SELECT * FROM LAMBDA");

        OdbcConnection con = GetConnection(AS400Library.RAVONLIBT, Resources.Username, Resources.Password);
        DataTable dt = Retrieve(con, sql.ToString());

        textBox1.Text = dt.Rows[0]["CHRCCS875"].ToString();
        textBox2.Text = dt.Rows[0]["VARCCS875"].ToString();
    }
    private void Insert_Click(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        sql.AppendFormat("INSERT INTO LAMBDA (CHRCCS875,VARCCS875) VALUES ('{0}','{1}'", textBox1.Text, textBox2.Text);

        OdbcConnection con = GetConnection(AS400Library.RAVONLIBT, Resources.Username, Resources.Password);
        ExecuteNonQuery(con, sql.ToString());
    }
    private OdbcConnection GetConnection(AS400Library library, string userName, string password)
    {
        return new OdbcConnection(string.Format("ODBC;DATABASE=QGPL;DSN=AS400-{0};UID={1};PWD={2};ALLOWUNSCHAR=0;", library.ToString(), userName, password));
    }
    private int ExecuteNonQuery(OdbcConnection connection, string sql)
    {

        try
        {
            var command = new OdbcCommand(sql);
            command.Connection = connection;
            connection.Open();

            return command.ExecuteNonQuery();
        }
        finally
        {
            connection.Close();
        }
    }
    private DataTable Retrieve(OdbcConnection connection, string sql)
    {
        var dataTable = new DataTable();
        OdbcCommand command = new OdbcCommand(sql);
        command.Connection = connection;
        OdbcDataAdapter adapter = new OdbcDataAdapter(command);

        try
        {
            connection.Open();
            adapter.Fill(dataTable);
        }
        finally
        {
            connection.Close();
        }

        return dataTable;
    }
C#代码:

CREATE TABLE RAVONLIBT.LAMBDA ( 
CHRCCS875 CHAR(5) CCSID 875 DEFAULT NULL , 
VARCCS875 VARCHAR(5) CCSID 875 DEFAULT NULL )   
private void Retrieve_Click(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        sql.AppendFormat("SELECT * FROM LAMBDA");

        OdbcConnection con = GetConnection(AS400Library.RAVONLIBT, Resources.Username, Resources.Password);
        DataTable dt = Retrieve(con, sql.ToString());

        textBox1.Text = dt.Rows[0]["CHRCCS875"].ToString();
        textBox2.Text = dt.Rows[0]["VARCCS875"].ToString();
    }
    private void Insert_Click(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        sql.AppendFormat("INSERT INTO LAMBDA (CHRCCS875,VARCCS875) VALUES ('{0}','{1}'", textBox1.Text, textBox2.Text);

        OdbcConnection con = GetConnection(AS400Library.RAVONLIBT, Resources.Username, Resources.Password);
        ExecuteNonQuery(con, sql.ToString());
    }
    private OdbcConnection GetConnection(AS400Library library, string userName, string password)
    {
        return new OdbcConnection(string.Format("ODBC;DATABASE=QGPL;DSN=AS400-{0};UID={1};PWD={2};ALLOWUNSCHAR=0;", library.ToString(), userName, password));
    }
    private int ExecuteNonQuery(OdbcConnection connection, string sql)
    {

        try
        {
            var command = new OdbcCommand(sql);
            command.Connection = connection;
            connection.Open();

            return command.ExecuteNonQuery();
        }
        finally
        {
            connection.Close();
        }
    }
    private DataTable Retrieve(OdbcConnection connection, string sql)
    {
        var dataTable = new DataTable();
        OdbcCommand command = new OdbcCommand(sql);
        command.Connection = connection;
        OdbcDataAdapter adapter = new OdbcDataAdapter(command);

        try
        {
            connection.Open();
            adapter.Fill(dataTable);
        }
        finally
        {
            connection.Close();
        }

        return dataTable;
    }
AS400表格记录

在每条记录的2个字段中插入了相同的字符串。第一条记录的值是通过复制粘贴插入的,第二条记录的值是使用我的C#应用程序插入的

我做了以下实验:

 UNICODESQL=1
  • 从lambda可读的第一条记录中读取数据库中的值。我已将这些值存储在textbox1和textbox2中。我能很清楚地看到兰姆达
  • 我使用textbox1和texbox2中出现的值(从第一步开始)向数据库添加了一条新记录,我发现这些值没有正确保存(请看第二条记录)
  • 显然,将第二条记录检索到textbox1和textbox2并不能正确显示lambda

    我的问题是:如何使用C#应用程序在AS400表中保存希腊和英语字符的混合。 我是否需要以某种格式定义表中的列? 是否需要使用其他提供程序保存到数据库?
    我是否需要从我的C#应用程序中检测lambda字符并以不同的方式将其保存到数据库中?

    存储混合语言的最佳方法是在unicode列中

    假设IBM的最新版本,我使用
    国家字符
    类型

    CREATE TABLE RAVONLIBT.LAMBDA ( 
        CHRCCS875 NCHAR(5) DEFAULT NULL , 
        VARCCS875 NVARCHAR(5) DEFAULT NULL ) 
    
    在旧版本上,您可以显式设置使用
    GRAPHIC
    类型和
    ccsid1200

    CREATE TABLE RAVONLIBT.LAMBDA ( 
       CHRCCS875 GRAPHIC(5) CCSID 1200 DEFAULT NULL , 
       VARCCS875 VARGRAPIC(5) CCSID 1200 DEFAULT NULL ) 
    

    无论使用哪种方法,都会得到相同的结果,即UTF-16列。

    事实上,正如@jmarkmurphy所说,CCSID 875足以容纳希腊字符。 为了使其通过代码工作,需要在连接字符串中添加以下内容:

     UNICODESQL=1
    
    您可以在以下网址找到更多信息:

    使用ODBC连接时,连接字符串为:

    private OdbcConnection GetConnection(AS400Library library, string userName, string password)
    {
        return new OdbcConnection(string.Format("ODBC;DATABASE=QGPL;DSN=AS400-{0};UID={1};PWD={2};ALLOWUNSCHAR=0;UNICODESQL=1;", library.ToString(), userName, password));
    }
    

    显然,CCSID 875足以容纳所需的字符,因为它们位于第一行。我想知道ODBC连接,以及它的字符集。另外,您的windows应用程序使用什么字符集?问题可能是应用程序字符集与CCSID 875之间的转换。