已有56人关注
投票结果不能显示
发表在C#图书答疑 2012-07-25
是否精华
版块置顶:
《C#从入门到精通》第2版P373,“投上一票”和“投票结果”均运行不了!一个提示“用户sa登录失败”,另一个“对象名 'tb_vote' 无效”。请各位高手指点迷津,我已经在网上查了N个贴子,结果试会都不成功,困挠了好几天,相当期待早点扫除拦路虎……
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
namespace Test10
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private int Sum;
        SqlConnection conn;
        private void CreateImage()
        {
            conn = new SqlConnection("server=PC-201010100717;database=db_22;uid=sa;pwd=abc123");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select sum(票数) from tb_vote", conn);
            Sum = (int)cmd.ExecuteScalar();
            SqlDataAdapter sda = new SqlDataAdapter("select * from tb_vote", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            int TP1 = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());//第一个选项的票数
            int TP2 = Convert.ToInt32(ds.Tables[0].Rows[1][2].ToString());//第二个选项的票数
            int TP3 = Convert.ToInt32(ds.Tables[0].Rows[2][2].ToString());//第三个选项的票数
            int TP4 = Convert.ToInt32(ds.Tables[0].Rows[3][2].ToString());//第四个选项的票数
            float tp1 = Convert.ToSingle(Convert.ToSingle(TP1) * 100 / Convert.ToSingle(Sum));
            float tp2 = Convert.ToSingle(Convert.ToSingle(TP2) * 100 / Convert.ToSingle(Sum));
            float tp3 = Convert.ToSingle(Convert.ToSingle(TP3) * 100 / Convert.ToSingle(Sum));
            float tp4 = Convert.ToSingle(Convert.ToSingle(TP4) * 100 / Convert.ToSingle(Sum));
            int width = 300, height = 300;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            try
            {
                g.Clear(Color.White);
                Brush brush1 = new SolidBrush(Color.White);
                Brush brush2 = new SolidBrush(Color.Black);
                Brush brush3 = new SolidBrush(Color.Red);
                Brush brush4 = new SolidBrush(Color.Green);
                Brush brush5 = new SolidBrush(Color.Orange);
                Brush brush6 = new SolidBrush(Color.DarkBlue);
                Font font1 = new Font("Courier New", 16, FontStyle.Bold);
                Font font2 = new Font("Courier New", 8);
                g.FillRectangle(brush1, 0, 0, width, height);    //绘制背景图
                g.DrawString("投票结果", font1, brush2, new Point(90, 20));
                Point p1 = new Point(70, 50);
                Point p2 = new Point(230, 50);
                g.DrawLine(new Pen(Color.Black), p1, p2);
                //绘制文字
                g.DrawString("用一生下载你:", font2, brush2, new Point(10, 80));
                g.DrawString("一生所爱:", font2, brush2, new Point(32, 110));
                g.DrawString("芸烨湘枫:", font2, brush2, new Point(32, 140));
                g.DrawString("情茧:", font2, brush2, new Point(54, 170));
                //绘制柱形图
                g.FillRectangle(brush3, 95, 80, tp1, 17);
                g.FillRectangle(brush4, 95, 110, tp2, 17);
                g.FillRectangle(brush5, 95, 140, tp3, 17);
                g.FillRectangle(brush6, 95, 170, tp4, 17);
                //绘制所有选项的票数显示
                g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80);  //绘制范围框
                g.DrawString("用一生下载你:" + TP1.ToString() + "票", font2, brush2, new Point(15, 220));
                g.DrawString("一生所爱:" + TP2.ToString() + "票", font2, brush2, new Point(150, 220));
                g.DrawString("芸烨湘枫:" + TP3.ToString() + "票", font2, brush2, new Point(15, 260));
                g.DrawString("情茧:" + TP4.ToString() + "票", font2, brush2, new Point(150, 260));
                pictureBox1.Image = bitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            CreateImage();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2012-07-25
沙发
读者朋友:
    您好,这两个问题不是代码造成的,是由于连接数据库造成的,将代码中的服务器名修改为你自己的SQL Server服务器名,用户名和密码修改为你登陆SQL Server数据库的用户名和密码;另外,确认一下数据库中是否存在tb_vote这个数据表。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照