回 帖 发 新 帖 刷新版面

主题:请教:C#Access数据库编程总提示未处理的类型的异常

请教:C#Access数据库编程总提示未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
窗体如下所述:
文本框4个,分别为CustomerID,CustomerName,Address,Phone分别对应客户编号,客户姓名,地址,电话。
命令按钮5个:分别为button1,cmdAdd,cmdDelete,cmdEdit,cmdSearch,分别对应数据库连接,增加,删除,修改,查找。
  
问题:新学C#,仿《精通C#程序设计》范例如后面所附源代码,点击【新增】按钮程序正常,点击其余按钮均显示未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。望高手指教。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace 数据库操作
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox CustomerID;
        private System.Windows.Forms.TextBox CustomerName;
        private System.Windows.Forms.TextBox Address;
        private System.Windows.Forms.TextBox Phone;
        private System.Windows.Forms.Button cmdAdd;
        private System.Windows.Forms.Button cmdDelete;
        private System.Windows.Forms.Button cmdExit;
        private System.Windows.Forms.Button cmdSearch;
        System.Data.OleDb.OleDbConnection objoledb=new System.Data.OleDb.OleDbConnection();
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
                  private void InitializeComponent()
                  {
                        略
        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        /// <summary>
        /// ///////////连接数据库//////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, System.EventArgs e)
        {
            string strPath;
            strPath =this.textBox1.Text;
             objoledb.ConnectionString= @"Provider=Microsoft.Jet.OLEDB.4.0;" +@"Data source= F:\db1.mdb";
            objoledb.Open();
        }

        private void oleDbConnection1_InfoMessage(object sender, System.Data.OleDb.OleDbInfoMessageEventArgs e)
        {
        
        }


      /// <summary>
        /// ///////////////////////增加//////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdAdd_Click(object sender, System.EventArgs e)
        {
            string strCustomerID=CustomerID.Text;
            string strCustomerName=CustomerName.Text; 
            string Address1=Address.Text;
            string strPhone=Phone.Text;

            string strSQL=
                "INSERT INTO gh"+
                "(CustomerID,CustomerName,Address,Phone)"+
                "VALUES"+
                "("+strCustomerID+",'"+strCustomerName+
                "','"+Address+"','"+strPhone+"')";

            OleDbCommand myCmd1=objoledb.CreateCommand();
            myCmd1.CommandText=strSQL;
            myCmd1.ExecuteNonQuery();//其他信息: ExecuteNonQuery 需要打开的并且可用的连接。该连接的当前状态是 Closed。
            MessageBox.Show("新增一笔数据完毕!!");
            
        }

        /// <summary>
        /// //////////////////删除//////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdDelete_Click(object sender, System.EventArgs e)
        {
            string strCustomerID=CustomerID.Text;
             string strSQL="DELETE FORM gh WHERE CustomerID="+CustomerID.Text;
            OleDbCommand myCmd2=new OleDbCommand(strSQL,objoledb);//其他信息: 未将对象引用设置到对象的实例。
            myCmd2.ExecuteNonQuery ( ) ;
            MessageBox.Show("删除客户编号="+strCustomerID+"的数据一笔!!");
        }


        /// <summary>
        /// //////////////////修改/////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdExit_Click(object sender, System.EventArgs e)
        {
            string strCustomerID=CustomerID.Text;
            string strCustomerName=CustomerName.Text; 
            string Address1=Address.Text;
            string strPhone=Phone.Text;

            string strSQL=
                "UPDATE gh SET"+
                "CustomerName='"+strCustomerName+"',"+
                "Address='"+Address1+"'"+
                "Phone='"+strPhone+"'"+
                "WHERE CustomerID="+strCustomerID+"";                       
            OleDbCommand myCmd1=objoledb.CreateCommand();
            myCmd1.CommandText=strSQL;
            myCmd1.ExecuteNonQuery();

            MessageBox.Show("更新客户编号="+strCustomerID+"的数据一笔!!");
        }


        /// <summary>
        /// ////////////////////查询////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private void cmdSearch_Click_1(object sender, System.EventArgs e)
        {
        
            if(CustomerID.Text==null)
            {
                MessageBox.Show("请输入欲查询的客户编号!!");
                return;

            }
            string strCustomerID=CustomerID.Text;
            string strSQL="SELECT CustomerID,CustomerName<Address,Phone"+"FROM Customers"+"WHERECustomerID='"+strCustomerID+"'";
            System.Data.OleDb.OleDbCommand myCmd=objoledb.CreateCommand();
            myCmd.CommandText=strSQL;
            System.Data.OleDb.OleDbDataReader myDr=myCmd.ExecuteReader();//其他信息: ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。
            //当由联接数据库按钮联接时显示:未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
            myDr.Read();
            CustomerName.Text=myDr["CustomerName"].ToString();
            Address.Text=myDr["Address"].ToString();
            Phone.Text=myDr["Phone"].ToString();
            myDr.Close();        
        }
    }
}

回复列表 (共1个回复)

沙发

是不是中国人写的,垃圾!
如果是外国进口的(应该可能性不大),垃圾中的垃圾.光看你最后一个按钮事件中的代码:
string strCustomerID=CustomerID.Text;
string strSQL="SELECT CustomerID,CustomerName<Address,Phone"+"FROM Customers"+"WHERECustomerID='"+strCustomerID+"'"; //查询代码这样连接很不规范,什么叫做SQL注入?
System.Data.OleDb.OleDbCommand myCmd=objoledb.CreateCommand();
myCmd.CommandText=strSQL;
System.Data.OleDb.OleDbDataReader myDr=myCmd.ExecuteReader();//其他信息: ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。
            //当由联接数据库按钮联接时显示:未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
///
///提示错误是很正常的, OleDbCommand 都没有指定用哪个数据库连接,更不要说OPEN了,都不知道这个写程序的人懂不懂程序!!!误人子弟!
            myDr.Read();//一般的写法是While(myDr.Read()),这样如果没有数据读出就会引起异常!没有常识。
            CustomerName.Text=myDr["CustomerName"].ToString();
            Address.Text=myDr["Address"].ToString();
            Phone.Text=myDr["Phone"].ToString();
            myDr.Close();        
//这里最后还需要Connection.Close()
垃圾书!!!!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册