就差把已有表导入到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 编辑 ]