我的代码保存,修改,删除三项运行错误,请指出代码错误,并能运行,谢谢!
using System;using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace c_程序设计
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
OleDbConnection myConnection;
OleDbCommand oledbCommand;
void MainFormLoad(object sender, EventArgs e)
{
myConnection=new OleDbConnection();
myConnection.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\subjects.mdb";
oledbCommand=new OleDbCommand();
oledbCommand.Connection=myConnection;
* from subjects";
myConnection.Open();
OleDbDataReader dreader=oledbCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(dreader.Read())
{
ListViewItem newItem=listView1.Items.Add(dreader["科目编号"].ToString().Trim());
newItem.SubItems.Add(dreader["科目名称"].ToString().Trim());
newItem.SubItems.Add(dreader["任课老师"].ToString().Trim());
newItem.SubItems.Add(dreader["上课日期"].ToString().Trim());
newItem.SubItems.Add(dreader["课时"].ToString().Trim());
}
dreader.Close();
}
void ListView1SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
textBox1.Text=listView1.SelectedItems[0].Text;
textBox2.Text=listView1.SelectedItems[0].SubItems[1].Text;
textBox3.Text=listView1.SelectedItems[0].SubItems[2].Text;
textBox4.Text=listView1.SelectedItems[0].SubItems[3].Text;
textBox5.Text=listView1.SelectedItems[0].SubItems[4].Text;
}
}
void Label1Click(object sender, EventArgs e)
{
}
void Button1Click(object sender, EventArgs e)
{
if(button1.Text=="添加")
{
button1.Text="保存";
textBox1.Text="";textBox2.Text="";
textBox3.Text="";textBox4.Text="";
textBox5.Text="";textBox1.Focus();
textBox1.ReadOnly=false;
textBox2.ReadOnly=false;
textBox3.ReadOnly=false;
textBox4.ReadOnly=false;
textBox5.ReadOnly=false;
button2.Enabled=false;
button3.Enabled=false;
}
else
{
= "INSERT INTO subjects(科目编号,科目名称,任课老师,上课日期,课时) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"')";
myConnection.Open();
int cmdresults = oledbCommand.ExecuteNonQuery();
myConnection.Close();
if(cmdresults==1)
{
textBox1.ReadOnly=true;
textBox2.ReadOnly=true;
textBox3.ReadOnly=true;
textBox4.ReadOnly=true;
textBox5.ReadOnly=true;
button2.Enabled=true;
button3.Enabled=true;
ListViewItem newItem=listView1.Items.Add(textBox1.Text);
newItem.SubItems.Add(textBox2.Text);
newItem.SubItems.Add(textBox3.Text);
newItem.SubItems.Add(textBox4.Text);
newItem.SubItems.Add(textBox5.Text);
button1.Text="添加";
MessageBox.Show("插入数据成功!");
}
else
MessageBox.Show("插入数据失败!");
}
}
void Button2Click(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
if(button2.Text=="修改")
{
button2.Text="保存";
textBox2.ReadOnly=false;
textBox3.ReadOnly=false;
textBox4.ReadOnly=false;
textBox5.ReadOnly=false;
button2.Enabled=true;
button3.Enabled=true;
textBox2.Focus();
}
else
{
string studID=listView1.SelectedItems[0].Text;
= "UPDATE subjects SET 科目名称='"+textBox1.Text+"',"+
"任课老师='"+textBox3.Text+"',"+
"上课日期='"+textBox4.Text+"',"+
"课时=='"+textBox5.Text+"' "+" where 科目编号='"+textBox1.Text+"'";
myConnection.Open();
int cmdresults=oledbCommand.ExecuteNonQuery();
myConnection.Close();
if(cmdresults==1)
{
textBox2.ReadOnly=true;
textBox3.ReadOnly=true;
textBox4.ReadOnly=true;
textBox5.ReadOnly=true;
button2.Enabled=true;
button3.Enabled=true;
listView1.SelectedItems[0].SubItems[1].Text=textBox2.Text;
listView1.SelectedItems[0].SubItems[2].Text=textBox3.Text;
listView1.SelectedItems[0].SubItems[3].Text=textBox4.Text;
listView1.SelectedItems[0].SubItems[4].Text=textBox5.Text;
button2.Text="修改";
MessageBox.Show("修改数据成功!");
}
else
MessageBox.Show("修改数据失败!");
}
}
}
void Button3Click(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
myConnection.Open();
//string studID=listView1.SelectedItems[0].Text;
= "delete from subjects where 科目编号='"+textBox1.Text+"'";;
oledbCommand.ExecuteNonQuery();
myConnection.Close();
listView1.Items.Remove(listView1.SelectedItems[0]);
textBox2.Text="";
textBox1.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
}
}
void TextBox1TextChanged(object sender, EventArgs e)
{
}
}
}
下面为数据表:
编号 科目编号 科目名称 任课老师 上课日期 课时
1 1101 数据结构 王老师 星期一 90
2 1102 编译原理 李老师 星期二 90
3 1103 组成原理 张老师 星期三 90
4 1104 计算机网络 刘老师 星期四 90
5 1105 计算机软件 季老师 星期五 90