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.OleDb; namespace excelData { public partial class Form1 : Form { public OleDbDataAdapter da; public OleDbConnection conn; public DataSet ds; public string num;
public void showExcel() { conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=admin.xls;Extended Properties=Excel 8.0"); string sql = "select * from [第一学期$]"; da = new OleDbDataAdapter(sql, conn); ds = new DataSet(); da.Fill(ds, "[第一学期$]"); ds.Tables["[第一学期$]"].PrimaryKey = new DataColumn[] { ds.Tables["[第一学期$]"].Columns["学号"] }; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { //if (textBox1.Text.Trim() != "" & textBox2.Text.Trim() != "") //{ num = textBox2.Text; showExcel();
Form1 f1 = new Form1(); num = f1.num; f1.showExcel(); DataRow dr = ds.Tables["[第一学期$]"].Rows.Find(num); f2_dg.DataSource = ds.Tables["[第一学期$]"]; }FORM1的值传给FORM2,但是运行时出现:未将对象引用设置到对象实例.
给你个例子
sqlstr = "select * from [nic$] "
注意表名后要加$
DataSet thisDataSet;
thisDataSet.Tables["表名"]就可以访问了啊
连数据你用DataSet吗??DataRelation还能够设置他们表之间的对应关系
不知道你想做成怎样的界面.
给你个方法.
先绑定学期,datagridview 加一个控件(由你定),点击后,传参数,再查出学期?表数据,再绑定另一个datagridview或另一个界面.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace excelData
{ public partial class Form1 : Form
{
public OleDbDataAdapter da;
public OleDbConnection conn;
public DataSet ds;
public string num;
public void showExcel()
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=admin.xls;Extended Properties=Excel 8.0");
string sql = "select * from [第一学期$]";
da = new OleDbDataAdapter(sql, conn);
ds = new DataSet();
da.Fill(ds, "[第一学期$]");
ds.Tables["[第一学期$]"].PrimaryKey = new DataColumn[] { ds.Tables["[第一学期$]"].Columns["学号"] };
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ try
{
//if (textBox1.Text.Trim() != "" & textBox2.Text.Trim() != "")
//{
num = textBox2.Text;
showExcel();
DataRow dr = ds.Tables["[学生表$]"].Rows.Find(num); if (textBox1.Text == "黄春娇" || textBox1.Text == "王国贸" || textBox1.Text == "赵秀琼" & textBox2.Text == "123" & comboBox1.SelectedItem == "班干部")
{
Form3 f3 = new Form3();
f3.Show();
}
else if (textBox1.Text == dr["姓名"].ToString() & textBox2.Text == dr["学号"].ToString() & comboBox1.SelectedItem == "学生")
{
Form2 f2 = new Form2();
f2.Show();
}
else if (textBox1.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
{
MessageBox.Show("请输入用户名", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (textBox2.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
{
MessageBox.Show("请输入密码", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (textBox1.Text == "" & textBox2.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
{
MessageBox.Show("请输入用户名和密码", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (textBox1.Text != dr["姓名"].ToString() || textBox2.Text != dr["学号"].ToString() & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部" & textBox1.Text != "黄春娇" || textBox1.Text != "王国贸" || textBox1.Text != "赵秀琼" || textBox2.Text == "123")
{
MessageBox.Show("用户名或密码错误", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
//else
//{
// MessageBox.Show("用户名或密码不能为空", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//}
//}
catch (Exception ex)
{
MessageBox.Show("错误原因:" + ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
} private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == "学生")
{
button1.Text = "查询";
}
else if (comboBox1.SelectedItem == "班干部")
{
button1.Text = "登陆";
}
}
}
}
这个 是我做的FORM1的代码!
{
Form1 f1 = new Form1();
num = f1.num;
f1.showExcel();
DataRow dr = ds.Tables["[第一学期$]"].Rows.Find(num);
f2_dg.DataSource = ds.Tables["[第一学期$]"];
}FORM1的值传给FORM2,但是运行时出现:未将对象引用设置到对象实例.