C# 连接access,运行时老师报错,找不到文件bin\debug\信息.mdb
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 开发数据库应用程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strcn = "provider=microsoft.jet.oledb.4.0;data source=|DataDirectory|\\信息.mdb";
OleDbConnection cn = new OleDbConnection(strcn);
if (textBox1.Text.Trim() == "" || comboBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || dateTimePicker1.Text.Trim() == ""||maskedTextBox1.Text.Trim()==""||textBox3.Text.Trim()=="")
MessageBox.Show("请将录入数据填写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
string sql1 = string.Format("insert into 个人信息表(学号,姓名,性别,年龄,生日,联系地址) values('{0}','{1}','{2}','{3}','{4}','{5}')", this.textBox1.Text.Trim(), this.comboBox1.Text.Trim(),this.textBox2.Text.Trim(), this.dateTimePicker1.Text.Trim(),this.maskedTextBox1.Text.Trim(),this.textBox3.Text.Trim());
cn.Open();
OleDbCommand cmd = new OleDbCommand(sql1, cn);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("录入成功!", "恭喜");
}
}
private void button2_Click(object sender, EventArgs e)
{
string strcn = "provider=microsoft.jet.oledb.4.0;data source=|DataDirectory|\\信息.mdb";
OleDbConnection cn = new OleDbConnection(strcn);
cn.Open();
string sql2 = string.Format("update 个人信息表 set 学号='{0}',姓名='{1}',性别='{2}',年龄='{3}',生日='{4}',联系地址='{5}' WHERE 学号='{6}';", this.textBox1.Text.Trim(), this.comboBox1.Text.Trim(), this.textBox2.Text.Trim(), this.dateTimePicker1.Text.Trim(), this.maskedTextBox1.Text.Trim(), this.textBox3.Text.Trim(), this.Tag.ToString());
OleDbCommand cmd = new OleDbCommand(sql2, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功!", "恭喜");
}
private void button3_Click(object sender, EventArgs e)
{
string strcn = "provider=microsoft.jet.oledb.4.0;data source=|DataDirectory|\\信息.mdb";
OleDbConnection cn = new OleDbConnection(strcn);
cn.Open();
string sql3 = string.Format("delete * FROM 个人信息表 WHERE 学号='{0}';", this.textBox1.Text.Trim());
OleDbCommand cmd = new OleDbCommand(sql3, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功!", "恭喜");
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.comboBox1.Text = "";
this.textBox2.Text = "";
this.dateTimePicker1.Text = "";
this.maskedTextBox1.Text = "";
this.textBox3.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
if (this.Text == "个人信息录入")
{
this.button1.Visible = true;
this.button2.Visible = false;
this.button3.Visible = false;
this.button4.Visible = true;
}
if (this.Text == "个人信息修改")
{
this.button1.Visible = false;
this.button2.Visible = true;
this.button4.Visible = false;
this.button3.Visible = false;
string strcn = "provider=microsoft.jet.oledb.4.0;data source=|DataDirectory|\\信息.mdb";
OleDbConnection cn = new OleDbConnection(strcn);
try
{
string sql2 = string.Format("SELECT 个人信息表.* FROM 个人信息表 WHERE 学号='{0}';", this.Tag.ToString());
this.Tag.ToString();
cn.Open();
OleDbCommand cmd = new OleDbCommand(sql2, cn);
OleDbDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
this.textBox1.Text = dr[0].ToString();
this.comboBox1.Text = dr[1].ToString();
this.textBox2.Text = dr[2].ToString();
this.dateTimePicker1.Text = dr[3].ToString();
this.maskedTextBox1.Text = dr[4].ToString();
this.textBox3.Text = dr[5].ToString();
}
catch
{
MessageBox.Show("没有该学号!", "对不起");
return;
}
finally
{
cn.Close();
}
}
}
}
}