点击添加按钮,执行如下相应:
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会弹出选择后的字段:国家但是输入表中没有的项:没有这门课
弹出的选中字段也是未找到的空字段,但是结果依然是已经被使用了。
请大家帮帮忙,看下问题到底出在哪里?
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会弹出选择后的字段:国家但是输入表中没有的项:没有这门课
弹出的选中字段也是未找到的空字段,但是结果依然是已经被使用了。
请大家帮帮忙,看下问题到底出在哪里?
解决方案 »
- hashtable键、值问题……help me,这个是为什么?
- 邮件接受发送
- winform中,如何把xml绑到listbox上
- C#中怎么判断文本框是否为空?
- 请问 如何把窗体上的控件的存保存为图片 请高手指教 谢谢
- 在客戶端如何動態隱藏和顯示一個table ?
- listview里照片点击弹出新窗体显示其放大照片传值问题,100分急救!!!!!!
- 用asp.net实现在线聊天的信息自动刷新如何实现的?
- Listview控件中的图标鼠标拖动更换图标位置
- 谁帮我看看有关TreeView编写的一段递归程序.
- 在winfrom 开发中怎么使用dll中的控件
- 正则表达式题以前过客有帮我解决过,但是我自己没搞好,现在有问题了希望大家帮下忙
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; }
The database file is locked
database is locked麻烦再帮忙看一下