当前位置:编程学习 > C#/ASP.NET >>

C#代码调试错误请帮忙指正,非常谢谢!(连接Access数据库)

程序名称:Form1.cs 程序功能:连接Access数据库(Windows窗体应用程序项目,在窗体Form1中添加一个dataGridView、openFileDialog控件(这个控件我拉不到from1上面,不知道怎么跟设计页面结合)、button 、groupBox等几个控件)

代码如下(调试中的错误也 用注释标记在其中,请帮忙指正 谢谢,初学C#,实现一本书上所述的功能学习):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Commona;
using System.Data.OleDb;
namespace ConnAccess
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //数据连接
        private OleDbConnection oleCon;
 
        //数据库连接状态
        private string state = null;
        //选择Access数据库文件
        private void btnChoose_Click(object sender, EventArgs e)
        { 
            //筛选字符串
            openFileDialog1.Filter = "*.mdb|*.MDB";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //显示对话框选择的文件路径字符串
                textAccPath.Text=this.openFileDialog1.FileName;
                //"连接"按钮可用
                btnConn.Enable = true;
 //错误(同下):当前上下文中不存在名称"btnConn" 

            }
        }
        //连接
       
        private void btnConn_Click(object sender,EventArgs e)
        {
            //得到文件物理路径
            string strPath = textAccPath.Text.ToString();
 //错误 3 当前上下文中不存在名称“textAccPath” C:\C#2008\ConnAccess\ConnAccess\Form1.cs
// 36 17 ConnAccess

            if (strPath != null)
            {
                try
                {
                    //连接字符串
                    string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Datasource='" + strPath + "'";
                    //初始化数据连接
                    oleCon = new OleDbConnection(ConStr);
                    //打开连接
                    oleCon.Open();
                    //得到数据连接当前状态
                    state = oleCon.State.ToString();
                    MessageBox.Show("连接数据库..状态:" + state);
                    if (state == "Open")
                    {
                        btnClose.Enabled = ture;
                        btnClose.Enabled = false;
//错误(同上):当前上下文中不存在名称"btnClose" 
//错误 当前上下文中不存在名称"ture" 
                    }
                }
                catch
                {
                    return;
                }

            }
        }
        //执行sql语句
        private void btnExec_Click(object sender, EventArgs e)
        {
            //得到当前数据连接状态
            state = oleCon.State.ToString();
            //如果为打开
            if (state == "Open")
            {
                //查询语句
                string strsql = txtSql.Text.ToString();
//错误 9 当前上下文中不存在名称“txtSql” C:\C#2008\ConnAccess\ConnAccess\Form1.cs 84 33 ConnAccess

                //执行sql语句
                oleDbDataAdapter oleDap = new OleDbDataAdaper(strsql, oleCon);
//错误 11 找不到类型或命名空间名称“OleDbDataAdaper”(是否缺少 using 指令或程序集引用?) C:\C#2008\ConnAccess\ConnAccess\Form1.cs 86 47 ConnAccess

                DataSet ds = new DataSet();
                //填充DataSet
                oleDap.Fill(ds, "table");
                //绑定到DataGridView 
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;

            }
            else
            {
                MessageBox.Show ("当前连接状态:"+state);
                btnConn.Enabed=true;

            }
 
        }

        //关闭连接
        private void btnClose_Click(object sender, EventArgs e)
        {
            //得到连接状态
            state = oleCon.State.ToString();
            if (state == "Open")
            {
                //关闭连接
                oleCon.Close();
                //释放资源
                oleCon.Dispose();
                MessageBox.Show("连接已成功被关闭");
                btnConn_Click.Enabled = true;
//错误 13 “ConnAccess.Form1.btnConn_Click(object, System.EventArgs)”是一个“方法”,这在给定的上下文中无效 C:\C#2008\ConnAccess\ConnAccess\Form1.cs 115 17 ConnAccess

            }
            else
            {
                MessageBox.Show("当前的连接为关闭状态!");
            }
            btnClose.Enable=false;
        
     //   private void Form1_Load(object sender, EventArgs e)
        

       
        }
    }
}


调试 form的程序,错误也会出现在一个名为:Form1.Designer.cs的程序中,不清楚这个和form1怎么结合在一起的,报错如下(由于字数限制,部分代码省略了):
namespace ConnAccess
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.button4 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.button3);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.textBox1);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(466, 52);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "数据库连接";
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(376, 14);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 4;
            this.button3.Text = "断开";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
 // 错误 1 “ConnAccess.Form1”不包含“button3_Click”的定义,并且找不到可接受类型为“ConnAccess.Form1”的第一个参数的扩展方法“button3_Click”(是否缺少 using 指令或程序集引用?) C:\C#2008\ConnAccess\ConnAccess\Form1.Designer.cs 70 64 ConnAccess
             //
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(295, 14);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "连接";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
 //错误 2 “ConnAccess.Form1”不包含“button2_Click”的定义,并且找不到可接受类型为“ConnAccess.Form1”的第一个参数的扩展方法“button2_Click”(是否缺少 using 指令或程序集引用?) C:\C#2008\ConnAccess\ConnAccess\Form1.Designer.cs 80 64 ConnAccess

        --------------------编程问答-------------------- 你的FORM设计页面内有btnConn之类的按钮么? --------------------编程问答-------------------- 有按钮的 ,我的openFileDialog1这个不知道怎么用到设计页面上,可能也跟错误有关,我的这个程序是直接点击form1左上方进去写的,是不是必须点击每个控件进去写?因为我感觉那样 好像整个设计功能不能有效连接了 ,是初学,请指正 请看我的设计图页面:
http://i.6.cn/cvbnm/c8/94/44/a2e43e53695530c237868a7766d90df5.jpg --------------------编程问答-------------------- 我已经解决了 谢谢接问题
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,