| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 530 人关注过本帖
标题:我的代码保存,修改,删除三项运行错误,请指出代码错误,并能运行,谢谢!
只看楼主 加入收藏
jisi123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
我的代码保存,修改,删除三项运行错误,请指出代码错误,并能运行,谢谢!
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
搜索更多相关主题的帖子: 程序设计 required Windows support 
2012-12-03 15:49
zxd543
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:内蒙古
等 级:贵宾
威 望:17
帖 子:453
专家分:2351
注 册:2012-4-12
收藏
得分:10 
你应该直接粘出你的错误信息 要不还得部署项目

马马虎虎 不吝赐教 我是路过蹭分滴
2012-12-03 23:15
jisi123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-3
收藏
得分:0 
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 subID=listView1.SelectedItems[0].Text;
                     = "UPDATE subjects SET  科目名称='"+subID+"',"+
                        "任课老师='"+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("修改数据失败!");
                }
            }
        }
错误的地方:
System.Data.OleDb.OleDbException: 标准表达式中数据类型不匹配。
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText
   at System.Data.OleDb.OleDbCommand.ExecuteCommand
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery
   at 季思_c_程序设计作业.MainForm.Button2Click in c:\Users\Administrator\Desktop\季思-c#程序设计作业\季思-c#程序设计作业\MainForm.cs:line 150
   at System.Windows.Forms.Control.OnClick
   at System.Windows.Forms.Button.OnClick
   at System.Windows.Forms.Button.OnMouseUp
   at System.Windows.Forms.Control.WmMouseUp
   at System.Windows.Forms.Control.WndProc
   at System.Windows.Forms.ButtonBase.WndProc
   at System.Windows.Forms.Button.WndProc
   at ControlNativeWindow.OnMessage
   at ControlNativeWindow.WndProc
   at System.Windows.Forms.NativeWindow.DebuggableCallback
   at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
   at ThreadContext.RunMessageLoopInner
   at ThreadContext.RunMessageLoop
   at System.Windows.Forms.Application.Run
   at 季思_c_程序设计作业.Program.Main in c:\Users\Administrator\Desktop\季思-c#程序设计作业\季思-c#程序设计作业\Program.cs:line 27
2012-12-04 22:34
jisi123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-3
收藏
得分:0 
我用access建的数据库
2012-12-04 22:35
Maick
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:32
帖 子:251
专家分:1314
注 册:2012-9-21
收藏
得分:10 
怎么楼主打开了就关闭呢
最后不用时才关闭啊..
关闭连接放错位置了..
2012-12-07 11:04
快速回复:我的代码保存,修改,删除三项运行错误,请指出代码错误,并能运行,谢 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018274 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved