| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:就差把已有表导入到dataGridView里面的。。。。怎么做啊
只看楼主 加入收藏
耐个
Rank: 1
等 级:新手上路
帖 子:19
专家分:7
注 册:2010-10-4
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
就差把已有表导入到dataGridView里面的。。。。怎么做啊
现有程序,红色部分就是自己不会做的地方,自己在窗体里面建立了一个dataGridView,当点击button6的时候把自己建立的商品表导入到里面显示。
里面的代码是自己的从别的同学那里复制过来的,自己改可是改不出来。。。他不肯给我讲。。。能有人帮我解决么?万谢
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication27
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {//取消按钮
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //连接数据库
            string bd_connection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\\My Documents\\商品表.mdb";
            OleDbConnection conn = new OleDbConnection(bd_connection);
            //打开数据
            conn.Open();

            //操作数据
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
             = "insert into 商品表(商品名称,商品单价,数量)values('" + textBox1.Text + "'," + textBox2.Text + "," + textBox3.Text + ")";
            cmd.ExecuteNonQuery();

            //关闭数据库
            conn.Close();

            
            
            
            MessageBox.Show("商品添加成功");
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
            //查询  
            string db_connection = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source =d:\\My Documents\\商品表.mdb";
            OleDbConnection conn = new OleDbConnection(db_connection);

            conn.Open();

            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection=conn;
             = "select * from 商品表 where 商品名称='" + textBox1.Text + "'";

            OleDbDataReader reader = cmd.ExecuteReader();
            
            if(reader.HasRows)
            {
               
               
                    reader.Read();
                    textBox2.Text = reader["商品单价"].ToString();
                    textBox3.Text = reader["数量"].ToString();
                    
            }
            else
            {
                MessageBox.Show("此商品不存在!");
                textBox1.Text = "";
                textBox1.Focus();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button1.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //修改
            string db_connection = "Provider=Microsoft.Jet.OlEDB.4.0;Data Source=d;\\My Documents\\商品表.mdb";
            OleDbCommand conn = new OleDbCommand(db_connection);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {

                button3.Enabled = true;

            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text!=""&&textBox2.Text!=""&&textBox3.Text!="")
                button1.Enabled=true;
        }

        private void button6_Click(object sender, EventArgs e)
        {

            string db_connection = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source =d:\\My Documents\\商品表.mdb";
            OleDbConnection conn = new OleDbConnection(db_connection);
            conn = new OleDbConnection(db_connection);
            conn.Open();
            string query1 = "select 商品名称,数量 from 商品表 order by 数量 desc";
            OleDbConnection msda = new OleDbDataAdapter(db_connection);
            msda = new OleDbDataAdapter(query1, conn);
            msda.Fill(ds, "商品表");
            dataGridView1.DataSource = ds.Tables["商品表"];
            conn.Close();

        }

        
    }
}
WindowsFormsApplication27.rar (46.62 KB)


[ 本帖最后由 耐个 于 2013-5-23 23:14 编辑 ]
2013-05-23 22:58
耐个
Rank: 1
等 级:新手上路
帖 子:19
专家分:7
注 册:2010-10-4
收藏
得分:0 
求解脱啊。。。坐等
2013-05-23 23:15
耐个
Rank: 1
等 级:新手上路
帖 子:19
专家分:7
注 册:2010-10-4
收藏
得分:0 
好吧我自己解脱了,看书找到了。。。
 private void button6_Click(object sender, EventArgs e)
        {

            string db_connection = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source =d:\\My Documents\\商品表.mdb";
            OleDbConnection conn = new OleDbConnection(db_connection);
            string query1 = "select 商品名称,数量 from 商品表 order by 数量 desc";
            OleDbDataAdapter sda = new OleDbDataAdapter(query1,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "商品表");
            dataGridView1.DataSource = ds.Tables[0];
            
        }
2013-05-23 23:23
快速回复:就差把已有表导入到dataGridView里面的。。。。怎么做啊
数据加载中...
 
   



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

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