C# 在AS400表中插入一个字符串,该字符串包含来自C的英语和希腊语字符的混合#
我试图在AS400表中插入一条新记录,其字符串值为希腊和英语字符的混合值 表格结构: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 =
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
我是否需要从我的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之间的转换。