当前位置:编程学习 > VB >>

大家帮我看下,谢谢了,老显示列名无效

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;

namespace Window1
{
    public partial class AddQuestion : Form
    {
        public AddQuestion()
        {
            InitializeComponent();
        }

        private void rdoA_Click(object sender, EventArgs e)
        {
            string answer = ((RadioButton)sender).Text;
            MessageBox.Show("你选择了答案" + answer, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void rdoDifficult_Click(object sender, EventArgs e)
        {
            string answer = ((RadioButton)sender).Text;
            MessageBox.Show("你选择了难度为" + answer, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string question = txtQusetion.Text;
            string a = txtOptionA.Text;
            string b = txtOptionB.Text;
            string c = txtOptionC.Text;
            string d = txtOptionD.Text;
            string answer = answer1();
            string difficulty = difficulty1();
            int subject = subject1();
            string sql = string.Format(@"insert into Question(Question,OptionA,OptionB,OptionC,OptionD,Answer,Difficulty,SubjectId)
            values('{0}','{1}','{2}','{3}','{4}','{5}',{6},{7})", question, a, b, c, d, answer, difficulty, subject);

            try
            {

                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                int num = command.ExecuteNonQuery();
                if (num != 1)
                {
                    MessageBox.Show("添加失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show("添加成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }

        }
        private string answer1()
        {
            string answer1 = "";
            if (rdoA.Checked)
            {
                answer1 = rdoA.Text;

            }
            else if (rdoB.Checked)
            {
                answer1 = rdoB.Text;
            }
            else if (rdoC.Checked)
            {
                answer1 = rdoC.Text;
            }
            else if (rdoD.Checked)
            {
                answer1 = rdoD.Text;
            }
            return answer1;

        }
        private string difficulty1()
        {
            string difficulty = "";
            if (rdoDifficult.Checked)
            {
                difficulty = rdoDifficult.Text;

            }
            else if (rdoNormal.Checked)
            {
                difficulty = rdoNormal.Text;
            }
            else if (rdoEasy.Checked)
            {
                difficulty = rdoEasy.Text;
            }
            return difficulty;
        
        }
        private int subject1()
        {
            int subject = 0;
            string sql = string.Format("select SubjectId from Subject where SudjectName='{0}'", comboBox1.Text);
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader datareader = command.ExecuteReader();
                if (datareader.Read())
                {
                    subject = (int)datareader[0];
                }
                datareader.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            return subject;

        }

        private void AddQuestion_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select SubjectName from Subject");
            SqlCommand command = new SqlCommand(sql, DBHelper.connection);
            try
            {
                DBHelper.connection.Open();
                SqlDataReader datareader = command.ExecuteReader();
                string kuMu = "";
                while (datareader.Read())
                {
                    kuMu = (string)datareader[0];
                    comboBox1.Items.Add(kuMu);
                }
                datareader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }
    }
}

老显示列名StudentName列名无效,我快晕死了,列名和数据库里都核对过了,没错呀 --------------------编程问答-------------------- SudjectName? d<>b --------------------编程问答-------------------- patrickkong大哥,说具体点好么?我看不懂你这是什么意思?本人刚学,不怎么懂,谢谢了 --------------------编程问答-------------------- SudjectName 是错误的,应该是SubjectName --------------------编程问答-------------------- 你给的两个都是一样的呀 --------------------编程问答-------------------- 我的妈呀,眼神,一个是d,一个是b --------------------编程问答-------------------- 不好意思,一个字母错了,犯了个低级错误,不好意思,非常感谢,终于找出来了,刚才试过了,ok了,谢谢了 --------------------编程问答--------------------
引用 5 楼 patrickkong 的回复:
我的妈呀,眼神,一个是d,一个是b


引用 6 楼 zhengbo19870409 的回复:
不好意思,一个字母错了,犯了个低级错误,不好意思,非常感谢,终于找出来了,刚才试过了,ok了,谢谢了



--------------------编程问答-------------------- 飘过 --------------------编程问答-------------------- 楼主快结贴吧。
猩猩们饿着呐。
补充:VB ,  网络编程
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,