点击添加按钮,执行如下相应:  
      
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxCourseName.Text == "")
            {
                MessageBox.Show("请先输入课程名称");
                return;
            }            if (EnWordDB.isHaveCourse(textBoxCourseName.Text)==1)
            {
                MessageBox.Show("该课程名已被使用");
                return;            }
            EnWordDB.addCourse(textBoxCourseName.Text);            
            MessageBox.Show("添加成功");
            textBoxCourseName.Text = "";            tempTableAdapter.Update(this.enWordDataSetTempCourse.Temp);
        }isHaveCourse函数如下:(检查文本输入框中的字符串是否再表中存在,存在返回1,不存在返回0)        public static int isHaveCourse(string coursename)
        {
            SQLiteConnection cn = new SQLiteConnection();
            cn.ConnectionString = datasource;
            cn.Open();
            string i="";
            string sql = "SELECT CourseName FROM Courses WHERE CourseName='" + coursename + "'";
      
            SQLiteCommand cmd = new SQLiteCommand(sql);
            cmd.Connection = cn;
            SQLiteDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
                i = dr.GetValue(0).ToString();
            MessageBox.Show(i);//这里输入字符串当时的值.
            cn.Close();
            if (i==null)
                return 0;
            else
                return 1;
        }
addCourse函数如下:
        public static void addCourse(string courseName)
        {
            int i = -1;//初始化Id号,方便检索。
            
            //更新[Courses]
            SQLiteConnection cn = new SQLiteConnection();
            cn.ConnectionString = datasource;
            cn.Open();
            string sql = "INSERT into Courses (CourseName) values ('" + courseName + "')";
            SQLiteCommand cmd = new SQLiteCommand(sql);
            cmd.Connection = cn;
            cmd.ExecuteNonQuery();
            cn.Close();            //Course-> CourseID
            cn.Open();
            sql = "SELECT CourseID FROM Courses WHERE CourseName='" + courseName + "'";
            cmd = new SQLiteCommand(sql);
            cmd.Connection = cn;
            SQLiteDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
                i = int.Parse(dr.GetValue(0).ToString());
            cn.Close();            //Temp -> Words
            cn.Open();
            sql = "insert into words (Vocabulary, Explanation,CourseID ) select Vocabulary,Explanation,'" + i + "' From Temp";
            cmd = new SQLiteCommand(sql);
            cmd.Connection = cn;
            cmd.ExecuteNonQuery();
            cn.Close();
 
        }Courses表设计如下:
courseID   CourseName
0 数字
1 国家
2 添加添加已有的课程名:国家在isHaveCourse函数里设置的messageBox.show会弹出选择后的字段:国家但是输入表中没有的项:没有这门课
弹出的选中字段也是未找到的空字段,但是结果依然是已经被使用了。
请大家帮帮忙,看下问题到底出在哪里?

解决方案 »

  1.   

    SQLiteDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                    i = dr.GetValue(0).ToString();
                MessageBox.Show(i);//这里输入字符串当时的值.
                cn.Close();
                if (i==null)
                    return 0;
                else
                    return 1;
    SQLiteDataReader dr = cmd.ExecuteReader();
    return dr.HasRows;if (EnWordDB.isHaveCourse(textBoxCourseName.Text)==1)
                {
                    MessageBox.Show("该课程名已被使用");
                    return;            }==>if (EnWordDB.isHaveCourse(textBoxCourseName.Text))
                {
                    MessageBox.Show("该课程名已被使用");
                    return;            }
      

  2.   

    您好,判断已经正确了。但是又有新的问题了= = 输入一个新的字符串课程名,可以提示添加成功。但是数据库里没有操作。再次输入那个相同的课程名,提示:已被使用。但是数据库里看依然没有。多添加几个 ,就显示:
    The database file is locked
    database is locked麻烦再帮忙看一下