先请大家不要见笑,本人初学C#,想通过实例先培养一下自己的兴趣,买了一本书,结果有些简约描述,弄的程序过不了.请大家帮忙看一下:
程序是一个简单地向Access里读写数据,开篇需要调用using System.Data.OleDb,已经做了,前面做过一个程序已通过测试,但这个例子还提示要:
定义公用变量,写下代码如下:
DataTable mytable;             //创建DataTable对象实例
OleDbConnection mycon;         //创建OleDbConnection对象实例
string pstr;                   //定义字符串型变理
就是这里不知道如何下手,请大家帮个忙啥,不要笑,初学正在培训学习兴趣,所以基础语法还没有过关,多包涵!

解决方案 »

  1.   

    DataTable mytable;            //创建DataTable对象实例 
    OleDbConnection mycon;        //创建OleDbConnection对象实例 
    你的这些注释都不对,呵呵,现在只是声明,并没有创建实例,其实想想很简单。
    你这样想,我现在需要一个DataTable,那么我创建一个不就行了么,于是创建一个
    DataTable mytable =new DataTable();
    你需要其他的,这样创建就行了,也可以先声明,需要使用的时候创建,一旦你理解了整个需要做的事情的时候,就可以开始对代码以及结构进行调整或者对设计进行调整了。
      

  2.   

    整个代码如下,不知道哪里错了,请大家指点一下:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;namespace Case04_7
    {
        public partial class Form1 : Form
        {
            DataTable mytable = new DataTable();               //创建DataTable对象实例
            OleDbConnection mycon = new OleDbConnection();     //创建OleDbConnection对象实例
            public string pstr;        private void Showdata()
            {
                string mypath = Application.StartupPath + "\\mydata.mdb";
                string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mypath;
                mycon =new OleDbConnection(constr);
                string mysql="select * from person";
                OleDbDataAdapter myada = new OleDbDataAdapter(mysql, mycon);
                mytable=new DataTable();
                myada.Fill(mytable);
                dataGridView1.DataSource=mytable.DefaultView;
                mycon.Close();
            }        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                Showdata();
                textBox1.Text = mytable.Rows[0][1].ToString();
                textBox2.Text = mytable.Rows[0][2].ToString();
                textBox3.Text = mytable.Rows[0][3].ToString();
            }        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                pstr = mytable.Rows[e.RowIndex][0].ToString();
                textBox1.Text = mytable.Rows[e.RowIndex][1].ToString();
                textBox2.Text = mytable.Rows[e.RowIndex][2].ToString();
                textBox3.Text = mytable.Rows[e.RowIndex][3].ToString();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="")
                {
                    MessageBox.Show("修改数据记录信息不完整,重新输入!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    mycon.Open();
                    string myupdate = "update person set 用户名='"+textBox1.Text+"',密码='"+textBox2.Text+"',权限="+Convert.ToInt32(textBox3.Text)+" where 会员ID="+Convert.ToInt32(pstr);
                    OleDbCommand mycon = new OleDbCommand(myupdate,mycon);
                    mycon.ExecuteNonQuery();
                    mycon.Close();
                    mycon.Dispose();
                    Showdata();
                    MessageBox.Show("已成功修改该条数据记录信息!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
                Application.Exit();
            }
        }
    }
      

  3.   

    求求大家帮我看一看吧,现在编译时
    mycon总是有问题,不知道问题出在哪里,代码全在上面了!
      

  4.   

    OleDbConnection mycon = new OleDbConnection();    //创建OleDbConnection对象实例 
    mycon =new OleDbConnection(constr); 里面是有问题的,并且你的很多Open什么的写的也不正确正常应该是这样:
    声明连接Connection,new实例(指明连接),打开,使用,关闭,你看你的符合这个顺序么
      

  5.   

    下面的你试试,具体我没有编译,直接手写的using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Data.OleDb; namespace Case04_7 

        public partial class Form1 : Form 
        { 
            DataTable mytable = new DataTable();              //创建DataTable对象实例 
             OleDbConnection mycon = null;//声明 
             public string pstr; 
            private void Open()
            {
                string mypath = Application.StartupPath + "\\mydata.mdb"; 
                string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mypath; 
                mycon =new OleDbConnection(constr); 
                mycon.Open();
            }
            private void Showdata() 
            { 
                Open();
                string mysql="select * from person"; 
                OleDbDataAdapter myada = new OleDbDataAdapter(mysql, mycon); 
                mytable=new DataTable(); 
                myada.Fill(mytable); 
                dataGridView1.DataSource=mytable.DefaultView; 
                mycon.Close(); 
            }         public Form1() 
            { 
                InitializeComponent(); 
            }         private void Form1_Load(object sender, EventArgs e) 
            { 
                Showdata(); 
                textBox1.Text = mytable.Rows[0][1].ToString(); 
                textBox2.Text = mytable.Rows[0][2].ToString(); 
                textBox3.Text = mytable.Rows[0][3].ToString(); 
            }         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
            { 
                pstr = mytable.Rows[e.RowIndex][0].ToString(); 
                textBox1.Text = mytable.Rows[e.RowIndex][1].ToString(); 
                textBox2.Text = mytable.Rows[e.RowIndex][2].ToString(); 
                textBox3.Text = mytable.Rows[e.RowIndex][3].ToString(); 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                if (textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="") 
                { 
                    MessageBox.Show("修改数据记录信息不完整,重新输入!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
                } 
                else 
                { 
                    Open(); 
                    string myupdate = "update person set 用户名='"+textBox1.Text+"',密码='"+textBox2.Text+"',权限="+Convert.ToInt32(textBox3.Text)+" where 会员ID="+Convert.ToInt32(pstr); 
                    OleDbCommand mycon = new OleDbCommand(myupdate,mycon); 
                    mycon.ExecuteNonQuery(); 
                    mycon.Close(); 
                    mycon.Dispose(); 
                    Showdata(); 
                    MessageBox.Show("已成功修改该条数据记录信息!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                } 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                this.Close(); 
                Application.Exit(); 
            } 
        } 
      

  6.   

    8楼的方法也试过了,还是不得行,按照我本身的代码,编译时提示如下错误:
    错误 1 局部变量“mycon”在声明之前无法使用。局部变量的声明隐藏字段“Case04_7.Form1.mycon”。 F:\我的文档\Visual Studio 2008\Projects\Case04_7\Case04_7\Form1.cs 61 17 Case04_7
    错误 2 与“System.Data.OleDb.OleDbCommand.OleDbCommand(string, System.Data.OleDb.OleDbConnection)”最匹配的重载方法具有一些无效参数 F:\我的文档\Visual Studio 2008\Projects\Case04_7\Case04_7\Form1.cs 63 38 Case04_7错误 3 参数“2”: 无法从“System.Data.OleDb.OleDbCommand”转换为“System.Data.OleDb.OleDbConnection” F:\我的文档\Visual Studio 2008\Projects\Case04_7\Case04_7\Form1.cs 63 64 Case04_7错误 4 “System.Data.OleDb.OleDbCommand”不包含“Close”的定义,并且找不到可接受类型为“System.Data.OleDb.OleDbCommand”的第一个参数的扩展方法“Close”(是否缺少 using 指令或程序集引用?) F:\我的文档\Visual Studio 2008\Projects\Case04_7\Case04_7\Form1.cs 65 23 Case04_7
      

  7.   

    自己好好看看么,晕,同一个变量声明了几次,还不是一个类型的。OleDbCommand mycon = new OleDbCommand(myupdate,mycon); 
                    mycon.ExecuteNonQuery(); 
                    mycon.Close(); 
                    mycon.Dispose(); 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Data.OleDb; namespace Case04_7 

        public partial class Form1 : Form 
        { 
            DataTable mytable = new DataTable();              //创建DataTable对象实例 
             OleDbConnection mycon = null;//声明 
             public string pstr; 
            private void Open()
            {
                string mypath = Application.StartupPath + "\\mydata.mdb"; 
                string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mypath; 
                mycon =new OleDbConnection(constr); 
                mycon.Open();
            }
            private void Showdata() 
            { 
                Open();
                string mysql="select * from person"; 
                OleDbDataAdapter myada = new OleDbDataAdapter(mysql, mycon); 
                mytable=new DataTable(); 
                myada.Fill(mytable); 
                dataGridView1.DataSource=mytable.DefaultView; 
                mycon.Close(); 
            }         public Form1() 
            { 
                InitializeComponent(); 
            }         private void Form1_Load(object sender, EventArgs e) 
            { 
                Showdata(); 
                textBox1.Text = mytable.Rows[0][1].ToString(); 
                textBox2.Text = mytable.Rows[0][2].ToString(); 
                textBox3.Text = mytable.Rows[0][3].ToString(); 
            }         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
            { 
                pstr = mytable.Rows[e.RowIndex][0].ToString(); 
                textBox1.Text = mytable.Rows[e.RowIndex][1].ToString(); 
                textBox2.Text = mytable.Rows[e.RowIndex][2].ToString(); 
                textBox3.Text = mytable.Rows[e.RowIndex][3].ToString(); 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                if (textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="") 
                { 
                    MessageBox.Show("修改数据记录信息不完整,重新输入!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
                } 
                else 
                { 
                    Open(); 
                    string myupdate = "update person set 用户名='"+textBox1.Text+"',密码='"+textBox2.Text+"',权限="+Convert.ToInt32(textBox3.Text)+" where 会员ID="+Convert.ToInt32(pstr); 
                    OleDbCommand mycom = new OleDbCommand(myupdate,mycon); 
                    mycom.ExecuteNonQuery(); 
                    mycon.Close(); 
                    Showdata(); 
                    MessageBox.Show("已成功修改该条数据记录信息!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                } 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                this.Close(); 
                Application.Exit(); 
            } 
        } 

      

  8.   

     DataTable mytable = new DataTable();              //创建DataTable对象实例 
    OleDbConnection mycon = new OleDbConnection();    //创建OleDbConnection对象实例 这两个参数,你都new了两次,在第一个方法里,没有connection没有open就使用了你加我Q吧:378875518
      

  9.   

    比较自己的代码,才发现是将原来
    OleDbCommand mycom = new OleDbCommand(...); 
    mycom.ExecuteNonQuery(); 
    两处mycom写成mycon了.晕!