数据转换问题?难啊?请教!
- C# code
public partial class User_Default2 : System.Web.UI.Page{ public string sql, sqls, sqlm, AdInfo_ID, url, aids, info; public int uaid; public double sumprice; protected System.Data.SqlClient.SqlCommand cmd; protected System.Data.SqlClient.SqlConnection conn; public DataSet ds; Utils uitls = new Utils(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DateTime date = DateTime.Now; String YMD1 = date.ToShortDateString().ToString(); String YMD2 = date.AddDays(1.1).ToShortDateString().ToString(); CalendarInput1.Text = YMD1.ToString(); CalendarInput2.Text = YMD2.ToString(); } if (Session["user"] == null) Response.Redirect("Login_user.aspx"); else power_sql(); } private void power_sql() { conn = uitls.conn(); CheckAdInfoSort(); FirstBindData(); } public void CheckAdInfoSort() { string rq1, rq2, caller,callnum; rq1 = CalendarInput1.Text.Trim(); rq2 = CalendarInput2.Text.Trim(); caller = Session["user"].ToString(); callnum = uitls.FiledValue("select top 1 usernum from userinfo_cb where bak_phoneno='" + caller + "'"); if (rq1 == "" && rq2 == "") { sql = "select count(*) from SysFeeDate_CB where usernum='"+callnum+"' or callernum='"+caller+"' and fee>0"; sqls = "select starttime,usernum,calleenum,holdmin,fee*0.01 as fee,calltype from SysFeeDate_CB where usernum='" + callnum + "' or callernum='" + caller + "' and fee>0 order by id desc"; } else { //sql = "select count(*) from SysFeeDate_CB where (usernum='" + callnum + "' or callernum='" + caller + "') and starttime>'" + rq1 + "' and starttime<'" + rq2 + " 23:59:59' and fee>0"; //sqls = "select starttime,usernum,calleenum,holdmin,fee*0.01 as fee from SysFeeDate_CB where (usernum='" + callnum + "' or callernum='" + caller + "') and starttime>'" + rq1 + "' and starttime<'" + rq2 + " 23:59:59' and fee>0"; sql = "select count(*) from SysFeeDate_CB where starttime>'" + rq1 + "' and starttime<'" + rq2 + " 23:59:59' and fee>0 and ((calltype in (1,2) and callernum='"+caller+"') or (calleenum='"+caller+"' and calltype=0))"; sqls = "select starttime,usernum,calleenum,holdmin,fee*0.01 as fee,calltype from SysFeeDate_CB where starttime>'" + rq1 + "' and starttime<'" + rq2 + " 23:59:59' and fee>0 and ((calltype in (1,2) and callernum='"+caller+"') or (calleenum='"+caller+"' and calltype=0))"; } } public void FirstBindData() { try { cmd = new SqlCommand(sql, conn); conn.Open(); pager.RecordCount = (int)cmd.ExecuteScalar(); if (pager.RecordCount == 0) Label1.Text = "所选时间内无记录"; else { conn.Close(); Label1.Text = ""; } } catch (Exception ex) { //Response.Write(sqls); Label1.Text = ex.Message; } BindData(); } public void BindData() { try { cmd = new SqlCommand(sqls, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); ds = new DataSet(); adapter.Fill(ds, pager.PageSize * (pager.CurrentPageIndex - 1), pager.PageSize, "SLC_call"); adapter.Fill(ds, "result"); for (int i = 0; i < ds.Tables["SLC_call"].Rows.Count; i++) //循环表记录,进行更改字段内容 { string memo; memo = ds.Tables["SLC_call"].Rows[i]["calltype"].ToString(); if (memo =="2") ds.Tables["SLC_call"].Rows[i]["calltype"] = "是"; else ds.Tables["SLC_call"].Rows[i]["calltype"] = "否"; } DataGrid1.DataSource = ds.Tables["SlC_call"]; DataGrid1.DataBind(); } catch (Exception ex) { //Response.Write("<script>alert('错误的操作,请查看您的选择日期或与网站管理人员联系.')</script>"); Response.Write(ex.Message); } AddCustomText(); } }
问题如下:
for (int i = 0; i < ds.Tables["SLC_call"].Rows.Count; i++) //循环表记录,进行更改字段内容
{
string memo;
memo = ds.Tables["SLC_call"].Rows[i]["calltype"].ToString();
if (memo =="2")
ds.Tables["SLC_call"].Rows[i]["calltype"] = "是";
else
ds.Tables["SLC_call"].Rows[i]["calltype"] = "否";
这里的'是'和'否'查询出来后,出错,显示:'Value was either too large or too small for an unsigned byte.Couldn't store <是> in calltype Column. Expected type is Byte'
我数据库中的calltype类型是tinyint 长度是1.现在是我想查询出来后,更改字段显示内容,可是用汉字就是不行,把这里的是和否换成汉字就可以的?
请问高手们如何转换?如何查询显示出来呢?
[解决办法]
我也是一名初学者,说的可能也不是很正确.
tinyint 是INT类型的,你赋值的类型的字符型.向你那样赋值应该是不行的.你可是用"1","0"来代替是或否.
[解决办法]
在"DataGrid1.DataSource = ds.Tables["SlC_call"];
DataGrid1.DataBind();"之前应该加上ds.AcceptChanges();显示调用AcceptChanges方法来提交对DataSet的更改.