界面如图:我现在要实现的功能是输出一个.txt文件,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.IO;namespace hkmessages
{
public partial class formMonthHk : Form
{
string ConnStr = "Provider=System.Data.Odbc;Dsn=housedbSZ;uid=sa";
//连接数据源
public formMonthHk()
{
InitializeComponent(); } private void formMonthHk_Load(object sender, EventArgs e)
{
//页面加载时
//string aa = "a b";
//MessageBox.Show(aa.Trim().Replace(" ","")); } private void btnOutPut_Click(object sender, EventArgs e)
{
int iyear = Convert.ToDateTime(txtYearMonth.Text.Trim()).Year;
int imonth = Convert.ToDateTime(txtYearMonth.Text.Trim()).Month;
string strWrite;
string hkCause = "";//划款原因
OdbcConnection mycn = new OdbcConnection(ConnStr);
string strSQL = "select hkmx.i_year,hkmx.i_month,hkmx.s_dkgrzh,hkmx.i_hk_flag,hkmx.s_grxm,hkmx.s_po_grxm,hkmx.dc_je,hkmx.dc_po_je,dkxx.s_grzh,dkxx.s_mobile_phone";
strSQL += " from yxwt_hkmx hkmx,nd_grdkxx_ma dkxx where hkmx.s_dkgrzh=dkxx.s_grzh and hkmx.i_year="+iyear+" and hkmx.i_month="+imonth;
try
{
StreamWriter hkInfo = new StreamWriter(txtPath.Text,false,Encoding.Default);
//StreamWriter kkInfo=new StreamWriter(
mycn.Open();
DataSet myds = new DataSet();
OdbcDataAdapter mydap = new OdbcDataAdapter(strSQL, mycn);
mydap.Fill(myds, "test");
DataTable mytb = myds.Tables[0];
if (myds.Tables.Count > 0 && mytb.Rows.Count>1)
{
for (int i = 0; i < mytb.Rows.Count; i++)
{
short hk_flag = Convert.ToInt16(mytb.Rows[i][3]);
string grxm = mytb.Rows[i][4].ToString();
string poxm = mytb.Rows[i][5].ToString();
decimal dc_je = Convert.ToDecimal(mytb.Rows[i][6]);
decimal dc_po_je = Convert.ToDecimal(mytb.Rows[i][7]);
string handtelephone = mytb.Rows[i][9].ToString();
if (hk_flag == 100 || hk_flag == 99 || hk_flag == 50)
{
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户于" + iyear + "年" + imonth + "月的按月划转" + dc_je + "元,您的配偶" + poxm.Trim().Replace(" ", "") + "按月划转" + dc_po_je + "元!";
}
switch (hk_flag)
{
case 10:
hkCause = "尊敬的" + grxm.Trim().Replace(" ","") + ",您的住房公积金帐户余额不足,本月的按月划转不成功!";
break;
case 11:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您上月未及时还款,本月的按月划转不成功!";
break;
case 12:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您已经还清贷款而自动停止本月的按月划转!";
break;
default:
break;
}
if (handtelephone != "" && handtelephone.Length == 11)
{
strWrite = handtelephone + " " + hkCause;
hkInfo.WriteLine(strWrite);
}
}
lblMsg.Text = "数据成功导出到目标文件!";
}
else
{
lblMsg.Text = "对不起,"+iyear+"年"+imonth+"月的信息不存在!";
}
mycn.Close();
}
catch(Exception ex)
{
mycn.Close();
lblMsg.Text = "信息导出失败!";
} }
}
}比如说: int iyear = Convert.ToDateTime(txtYearMonth.Text.Trim()).Year;//年
int imonth = Convert.ToDateTime(txtYearMonth.Text.Trim()).Month;//月iyear=2007 imonth=5
但执行到mydap.Fill(myds, "test");报这样的错误:
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver]String data code page conversion failed. Error in column 5.
请问这是什么错误???我今天解决了一下午也没搞定,网上也没有什么好的解决方法,郁闷的很!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.IO;namespace hkmessages
{
public partial class formMonthHk : Form
{
string ConnStr = "Provider=System.Data.Odbc;Dsn=housedbSZ;uid=sa";
//连接数据源
public formMonthHk()
{
InitializeComponent(); } private void formMonthHk_Load(object sender, EventArgs e)
{
//页面加载时
//string aa = "a b";
//MessageBox.Show(aa.Trim().Replace(" ","")); } private void btnOutPut_Click(object sender, EventArgs e)
{
int iyear = Convert.ToDateTime(txtYearMonth.Text.Trim()).Year;
int imonth = Convert.ToDateTime(txtYearMonth.Text.Trim()).Month;
string strWrite;
string hkCause = "";//划款原因
OdbcConnection mycn = new OdbcConnection(ConnStr);
string strSQL = "select hkmx.i_year,hkmx.i_month,hkmx.s_dkgrzh,hkmx.i_hk_flag,hkmx.s_grxm,hkmx.s_po_grxm,hkmx.dc_je,hkmx.dc_po_je,dkxx.s_grzh,dkxx.s_mobile_phone";
strSQL += " from yxwt_hkmx hkmx,nd_grdkxx_ma dkxx where hkmx.s_dkgrzh=dkxx.s_grzh and hkmx.i_year="+iyear+" and hkmx.i_month="+imonth;
try
{
StreamWriter hkInfo = new StreamWriter(txtPath.Text,false,Encoding.Default);
//StreamWriter kkInfo=new StreamWriter(
mycn.Open();
DataSet myds = new DataSet();
OdbcDataAdapter mydap = new OdbcDataAdapter(strSQL, mycn);
mydap.Fill(myds, "test");
DataTable mytb = myds.Tables[0];
if (myds.Tables.Count > 0 && mytb.Rows.Count>1)
{
for (int i = 0; i < mytb.Rows.Count; i++)
{
short hk_flag = Convert.ToInt16(mytb.Rows[i][3]);
string grxm = mytb.Rows[i][4].ToString();
string poxm = mytb.Rows[i][5].ToString();
decimal dc_je = Convert.ToDecimal(mytb.Rows[i][6]);
decimal dc_po_je = Convert.ToDecimal(mytb.Rows[i][7]);
string handtelephone = mytb.Rows[i][9].ToString();
if (hk_flag == 100 || hk_flag == 99 || hk_flag == 50)
{
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户于" + iyear + "年" + imonth + "月的按月划转" + dc_je + "元,您的配偶" + poxm.Trim().Replace(" ", "") + "按月划转" + dc_po_je + "元!";
}
switch (hk_flag)
{
case 10:
hkCause = "尊敬的" + grxm.Trim().Replace(" ","") + ",您的住房公积金帐户余额不足,本月的按月划转不成功!";
break;
case 11:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您上月未及时还款,本月的按月划转不成功!";
break;
case 12:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您已经还清贷款而自动停止本月的按月划转!";
break;
default:
break;
}
if (handtelephone != "" && handtelephone.Length == 11)
{
strWrite = handtelephone + " " + hkCause;
hkInfo.WriteLine(strWrite);
}
}
lblMsg.Text = "数据成功导出到目标文件!";
}
else
{
lblMsg.Text = "对不起,"+iyear+"年"+imonth+"月的信息不存在!";
}
mycn.Close();
}
catch(Exception ex)
{
mycn.Close();
lblMsg.Text = "信息导出失败!";
} }
}
}比如说: int iyear = Convert.ToDateTime(txtYearMonth.Text.Trim()).Year;//年
int imonth = Convert.ToDateTime(txtYearMonth.Text.Trim()).Month;//月iyear=2007 imonth=5
但执行到mydap.Fill(myds, "test");报这样的错误:
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver]String data code page conversion failed. Error in column 5.
请问这是什么错误???我今天解决了一下午也没搞定,网上也没有什么好的解决方法,郁闷的很!!
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver]String data code page conversion failed. Error in column 5
第5列转换失败
iyear=2006 imonth=5
可以正常输出.txt文件,是不是odbcdataApdater有问题?
生成的sql语句 也就是 strSQL 在数据库里执行一下
而不是 它之后的 for 循环出错?
和 不出错有什么明显差别
或者你采用Parameter附加参数的做法 基本不会错的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.IO;namespace test
{
public partial class Form1 : Form
{
string ConnStr = "Provider=System.Data.Odbc;Dsn=housedbSZ;uid=sa";
//连接数据源
public Form1()
{
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{
//页面加载时
//string aa = "a b";
//MessageBox.Show(aa.Trim().Replace(" ","")); }
private void button1_Click(object sender, EventArgs e)
{ int iyear = Convert.ToDateTime(textBox1.Text.Trim()).Year;
int imonth = Convert.ToDateTime(textBox1.Text.Trim()).Month;
string strWrite;
string hkCause = "";//划款原因 OdbcConnection mycn = new OdbcConnection(ConnStr);
string strSQL = "select hkmx.i_year,hkmx.i_month,hkmx.s_dkgrzh,hkmx.i_hk_flag,hkmx.s_grxm,hkmx.s_po_grxm,hkmx.dc_je,hkmx.dc_po_je,dkxx.s_grzh,dkxx.s_mobile_phone";
strSQL += " from yxwt_hkmx hkmx,nd_grdkxx_ma dkxx where hkmx.s_dkgrzh=dkxx.s_grzh and hkmx.i_year=" + iyear + " and hkmx.i_month=" + imonth;//+ " and dkxx.s_mobile_phone='13856081974'";
try
{
StreamWriter hkInfo = new StreamWriter(textBox2.Text);//, false, Encoding.Default);
//StreamWriter kkInfo=new StreamWriter(
mycn.Open();
OdbcCommand mycmd = new OdbcCommand(strSQL, mycn);
OdbcDataReader myreader = mycmd.ExecuteReader();
while(myreader.Read())
{ short hk_flag = Convert.ToInt16(myreader["i_hk_flag"]);
string grxm = myreader["s_grxm"].ToString();
string poxm = myreader["s_po_grxm"].ToString();
decimal dc_je = Convert.ToDecimal(myreader["dc_je"]);
decimal dc_po_je = Convert.ToDecimal(myreader["dc_po_je"]);
string handtelephone = myreader["s_mobile_phone"].ToString();
if (hk_flag == 100 || hk_flag == 99 || hk_flag == 50)
{
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户于" + iyear + "年" + imonth + "月的按月划转" + dc_je + "元,您的配偶" + poxm.Trim().Replace(" ", "") + "按月划转" + dc_po_je + "元!";
}
switch (hk_flag)
{
case 10:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户余额不足,本月的按月划转不成功!";
break;
case 11:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您上月未及时还款,本月的按月划转不成功!";
break;
case 12:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您已经还清贷款而自动停止本月的按月划转!";
break;
default:
break;
}
if (handtelephone != "" && handtelephone.Length == 11)
{
strWrite = handtelephone + " " + hkCause;
hkInfo.WriteLine(strWrite);
}
} //if (!myreader.Read())
//{
// lblMsg.Text = lblMsg.Text = "对不起," + iyear + "年" + imonth + "月的信息不存在!";
//}
#region
/*
DataSet myds = new DataSet();
OdbcDataAdapter mydap = new OdbcDataAdapter(strSQL, mycn);
mydap.Fill(myds, "test");
DataTable mytb = myds.Tables[0];
if (myds.Tables.Count > 0 && mytb.Rows.Count > 1)
{
for (int i = 0; i < mytb.Rows.Count; i++)
{
short hk_flag = Convert.ToInt16(mytb.Rows[i][3]);
string grxm = mytb.Rows[i][4].ToString();
string poxm = mytb.Rows[i][5].ToString();
decimal dc_je = Convert.ToDecimal(mytb.Rows[i][6]);
decimal dc_po_je = Convert.ToDecimal(mytb.Rows[i][7]);
string handtelephone = mytb.Rows[i][9].ToString();
if (hk_flag == 100 || hk_flag == 99 || hk_flag == 50)
{
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户于" + iyear + "年" + imonth + "月的按月划转" + dc_je + "元,您的配偶" + poxm.Trim().Replace(" ", "") + "按月划转" + dc_po_je + "元!";
}
switch (hk_flag)
{
case 10:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您的住房公积金帐户余额不足,本月的按月划转不成功!";
break;
case 11:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您上月未及时还款,本月的按月划转不成功!";
break;
case 12:
hkCause = "尊敬的" + grxm.Trim().Replace(" ", "") + ",您已经还清贷款而自动停止本月的按月划转!";
break;
default:
break;
}
if (handtelephone != "" && handtelephone.Length == 11)
{
strWrite = handtelephone + " " + hkCause;
hkInfo.WriteLine(strWrite);
}
}
lblMsg.Text = "数据成功导出到目标文件!";
}
else
{
lblMsg.Text = "对不起," + iyear + "年" + imonth + "月的信息不存在!"; }*/
#endregion mycn.Close(); }
catch (Exception ex)
{
mycn.Close();
lblMsg.Text = "信息导出失败!";
} }
}
}
现在的问题是,比如说数据库中有2000条记录,但程序执行后只能显示1500条(在.txt文件中)
select hkmx.i_year,hkmx.i_month,hkmx.s_dkgrzh,hkmx.i_hk_flag,convert(char,hkmx.s_grxm) as hkmx.s_grx