当前位置:编程学习 > C#/ASP.NET >>

C# 中不能插入2条记录,求救了

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.OleDb;

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

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

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

        //窗体加载事件
        private void AddQuestion_Load(object sender, EventArgs e)
        {
            //查询科目的SQl语句
            string sql = "select SubjectName from Subject";

            try
            {
                //创建Command对象
                OleDbCommand command = new OleDbCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();

                //执行查询
                OleDbDataReader dataReader = command.ExecuteReader();

                //循环读取记录
                while (dataReader.Read())
                {
                    cboSubject.Items.Add(dataReader["SubjectName"].ToString());
                }

                //关闭dataReader对象
                dataReader.Close();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            finally
            {
                DBHelper.connection.Close();
            }
        }

        //关闭事件
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //增加按钮事件,向数据库增加试题
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //验证输入
            if (ValidateInput())
            {
                int difficult = 0; //难度
                int subjectId = 0; //科目编号
                string answer = ""; //答案

                //查询科目Id的SQL语句
                string sql = string.Format("select SubjectID from Subject where SubjectName = '{0}'",cboSubject.Text);

                try
                {
                    //查询科目编号
                    OleDbCommand command = new OleDbCommand(sql, DBHelper.connection);
                    DBHelper.connection.Open();

                    OleDbDataReader dataReader = command.ExecuteReader();

                    if (dataReader.Read())
                    {
                        subjectId = (int)dataReader[0]; //科目编号
                    }
                    dataReader.Close();

                    //确定难度
                    if (rdoDifficult.Checked)
                    {
                        difficult = 3;
                    }
                    else if (rdoNormal.Checked)
                    {
                        difficult = 2;
                    }
                    else if (rdoEasy.Checked)
                    {
                        difficult = 1;
                    }

                    //确定答案
                    if (rdoA.Checked)
                    {
                        answer = rdoA.Text;
                    }
                    else if (rdoB.Checked)
                    {
                        answer = rdoB.Text;
                    }
                    else if (rdoC.Checked)
                    {
                        answer = rdoC.Text;
                    }
                    else if (rdoD.Checked)
                    {
                        answer = rdoD.Text;
                    }

                    //向数据库插入记录
                    sql = string.Format("insert into Question(Question,OptionA,optionB,OptionC,OptionD,Answer,Difficulty,SubjectID) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", txtQuestion.Text.Trim(), txtOptionA.Text.Trim(), txtOptionB.Text.Trim(), txtOptionC.Text.Trim(), txtOptionD.Text.Trim(), answer, difficult, subjectId);

                                        
                    //重新指定SQL命令
                    command.CommandText = sql;                    

                    int result = command.ExecuteNonQuery();

                    if (result == 1)
                    {
                        MessageBox.Show("增加记录成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show("增加记录失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                finally
                {
                    DBHelper.connection.Close();
                }

                //清空试题和选项中的内容
                txtQuestion.Text = "";
                txtOptionA.Text = "";
                txtOptionB.Text = "";
                txtOptionC.Text = "";
                txtOptionD.Text = "";
                rdoA.Checked = false;
                rdoB.Checked = false;
                rdoC.Checked = false;
                rdoD.Checked = false;
                rdoDifficult.Checked = false;
                rdoNormal.Checked = false;
                rdoEasy.Checked = false;
            }
        }

        //验证用户的输入,成功返回true,失败返回false
        private bool ValidateInput()
        {
            if (txtQuestion.Text.Trim() == "")
            {
                MessageBox.Show("请输入问题!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                txtQuestion.Focus();
                return false;
            }
            if (txtOptionA.Text.Trim() == "")
            {
                MessageBox.Show("请输入选项A!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                txtOptionA.Focus();
                return false;
            }
            if (txtOptionB.Text.Trim() == "")
            {
                MessageBox.Show("请输入选项B!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtOptionA.Focus();
                return false;
            }
            if (txtOptionC.Text.Trim() == "")
            {
                MessageBox.Show("请输入选项C!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtOptionA.Focus();
                return false;
            }
            if (txtOptionD.Text.Trim() == "")
            {
                MessageBox.Show("请输入选项D!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtOptionA.Focus();
                return false;
            }

            if (rdoA.Checked == false && rdoB.Checked == false && rdoC.Checked == false && rdoD.Checked == false)
            {
                MessageBox.Show("请选择答案!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                return false;
            }

            if (rdoDifficult.Checked == false && rdoNormal.Checked == false && rdoEasy.Checked == false)
            {
                MessageBox.Show("请选择难度!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                return false;
            }

            if (cboSubject.Text == "")
            {
                MessageBox.Show("请选择科目!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                cboSubject.Focus();
                return false;
            }
            return true;
        }
    }
}

具体的情况是:
编译可以通过,如果原来的是空表,就可以成功添加进一条记录;
但是想再添加第二条就不行了,也没有出错的提示
表中有一个自动编号的字段,设为主键

希望高手帮帮我,都搞了一整天 还是看不懂 --------------------编程问答--------------------
catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                } 


这里如果除了错 ,输出了message到控制台,
可你的程序是winform阿。

要看到错误,请输出到log,或者MessageBox.Show();

鉴定完毕。 --------------------编程问答-------------------- 修改数据库表为自动增量 --------------------编程问答-------------------- 谢谢你了,出现提示 

“由于将在索引、主关键字、或关系中创建重复的值,请求对表的改变没有成功。改变该字段中的或包含重复数据的字段中的数据,删除索引或重新定义索引以允许重复的值并再试一次”

把表中字段有索引“有(无重复)”的全部设置为“无”,现在可以正常运行了 

非常感谢 
补充:.NET技术 ,  其他语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,