SqlDataReader问题请大侠们帮忙指点迷津,不胜感激!
这段代码执行后老是提示,SqlDataReader dr = Com.ExecuteReader();附近有语法错误。可我对这个不是太明白,刚刚学.NET
这个代码是ASP.NET程序开发范例宝典(C#)上的源代码
我只改了部分数据库连接的部分,请大侠们帮忙指点迷津,不胜感激!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cmdtxt1 = "server=(local);database=db_13;Uid=sa;Pwd=hzdprm123!@#";
SqlConnection Con = new SqlConnection(cmdtxt1);
Con.Open();
SqlCommand Com = new SqlCommand("select * from tb_06 where ID=" + Request["ID"] + "", Con);
SqlDataReader dr = Com.ExecuteReader();
dr.Read();
string strContent = null;
if (dr.HasRows)
{
strContent = dr["Content"].ToString();
}
dr.Close();
decimal decNumAll = GetNumAll(strContent);//得到所有票数,C#在除法运算中需用decimal类型,否则将会把小数点后的数去掉
Response.Write(GetResult(strContent, decNumAll));
}
/// <summary>
/// 显示图象
/// </summary>
/// <param name="strContent">相关主题下的内容</param>
/// <param name="decNumAll">所有的票数</param>
/// <returns></returns>
public string GetResult(string strContent, decimal decNumAll)
{
string[] arrContent = strContent.Split('|');
string strBody = "<body style=text-align:center><table width=60% border=1 cellpadding=0 cellspacing=0 text-align:center>\n";
foreach (string strContentIN in arrContent)
{
string strItemName = strContentIN.Split(',')[1].ToString();//得到选项名称
decimal decItemNum = Convert.ToDecimal(strContentIN.Split(',')[0]);//得到选项的投票数
decimal decPercent = GetPercent(decItemNum, decNumAll) * 100;//得到百分比
string strPercent = decPercent.ToString();//将百分比转为字符型
if (strPercent.Length > 5)//如果百分比结果长度超过5位
{
strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为“00.00”
}
strBody += "<tr><td width=100 style=font-size:9pt;text-align:center>" + strItemName + "</td><td width=50 style=font-size:9pt;color=red;text-align:center>" + decItemNum.ToString() + "票</td><td style=font-size:9pt><img src=Images/bar1.gif height=10 width=" + strPercent + "%>" + strPercent + "%</td></tr>\n";
}
strBody += "</table></body>";
return strBody;
}
/// <summary>
/// 票数求和
/// </summary>
/// <param name="strNum">当前所要操作的字串</param>
/// <returns></returns>
public decimal GetNumAll(string strNum)
{
decimal decNumAll = 0;
string[] arrNum = strNum.Split('|');
foreach (string strNumIN in arrNum)
{
decNumAll += Convert.ToInt32(strNumIN.Split(',')[0].ToString());//截取第零位
}
return decNumAll;
}
/// <summary>