C#+ACCESS怎么连接,要可以增,删,查,改操作的
2个回答
展开全部
以下是后台代码,前台窗体上有3个按钮,一个dataGridView和几个TextBox。另,这里用到的是Access数据库,你只要将Access数据库文件放在解决方案的bin/Debug目录下就可以了。切记要添加的内容一定要和数据库表对应起来,否则会异常!
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;
using Microsoft.VisualBasic;
namespace 院系与班级管理
{
public partial class yxbj : Form
{
public yxbj()
{
InitializeComponent();
}
private void conn()
{
}
private void constring(string command)
{
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
OleDbConnection cnn = new OleDbConnection(cn);
string updatas = command;
OleDbCommand com = new OleDbCommand(updatas, cnn);
try
{
cnn.Open();
com.ExecuteNonQuery();
cnn.Close();
yxbj_Load(null, null);
cls();
}
catch (Exception)
{
MessageBox.Show("输入的不正确!");
}
finally
{
cnn.Close();
}
}
private DataTable dt;
private OleDbDataAdapter da;
private void button1_Click(object sender, EventArgs e)
{
string mycom = "insert into yxbj(yxbj_id,yxbj_name,yxbj_department,yxbj_major,yxbj_class) values( " + "'" + textBox1.Text + "'" + "," + "'" + textBox2.Text + "'" + "," + "'" + textBox3.Text + "'" + "," + "'" + textBox4.Text + "'" + "," + "'" + textBox5.Text + "')";
constring(mycom);
}//添加
private void yxbj_Load(object sender, EventArgs e)
{
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
OleDbConnection cnn = new OleDbConnection(cn);
string mysel = "Select yxbj_id as 学号,yxbj_name as 姓名,yxbj_department as 院系,yxbj_major as 专业,yxbj_class as 班级 from yxbj";
OleDbCommand com = new OleDbCommand(mysel, cnn);
dt = new DataTable();
da = new OleDbDataAdapter();
da.SelectCommand = com;
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void cls()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
string updates = "update yxbj set yxbj_name =" + "'" + textBox2.Text.Trim() + "',yxbj_department=" + "'" + textBox3.Text.Trim() + "',yxbj_major=" + "'" + textBox4.Text.Trim() + "',yxbj_class=" + "'" + textBox5.Text.Trim() + "' Where yxbj_id =" + "'" + textBox1.Text.Trim() + "'";
constring(updates);
}//修改
private void button3_Click(object sender, EventArgs e)
{
string del = "delete from yxbj where yxbj_id =" + "'" + textBox1.Text.Trim() + "'";
constring(del);
}//删除
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int x = e.RowIndex;
textBox1.Text = dt.Rows[x][0].ToString();
textBox2.Text = dt.Rows[x][1].ToString();
textBox3.Text = dt.Rows[x][2].ToString();
textBox4.Text = dt.Rows[x][3].ToString();
textBox5.Text = dt.Rows[x][4].ToString();
}
}
}
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;
using Microsoft.VisualBasic;
namespace 院系与班级管理
{
public partial class yxbj : Form
{
public yxbj()
{
InitializeComponent();
}
private void conn()
{
}
private void constring(string command)
{
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
OleDbConnection cnn = new OleDbConnection(cn);
string updatas = command;
OleDbCommand com = new OleDbCommand(updatas, cnn);
try
{
cnn.Open();
com.ExecuteNonQuery();
cnn.Close();
yxbj_Load(null, null);
cls();
}
catch (Exception)
{
MessageBox.Show("输入的不正确!");
}
finally
{
cnn.Close();
}
}
private DataTable dt;
private OleDbDataAdapter da;
private void button1_Click(object sender, EventArgs e)
{
string mycom = "insert into yxbj(yxbj_id,yxbj_name,yxbj_department,yxbj_major,yxbj_class) values( " + "'" + textBox1.Text + "'" + "," + "'" + textBox2.Text + "'" + "," + "'" + textBox3.Text + "'" + "," + "'" + textBox4.Text + "'" + "," + "'" + textBox5.Text + "')";
constring(mycom);
}//添加
private void yxbj_Load(object sender, EventArgs e)
{
string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\xjgl.mdb" + ";Persist Security Info=True";
OleDbConnection cnn = new OleDbConnection(cn);
string mysel = "Select yxbj_id as 学号,yxbj_name as 姓名,yxbj_department as 院系,yxbj_major as 专业,yxbj_class as 班级 from yxbj";
OleDbCommand com = new OleDbCommand(mysel, cnn);
dt = new DataTable();
da = new OleDbDataAdapter();
da.SelectCommand = com;
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void cls()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
string updates = "update yxbj set yxbj_name =" + "'" + textBox2.Text.Trim() + "',yxbj_department=" + "'" + textBox3.Text.Trim() + "',yxbj_major=" + "'" + textBox4.Text.Trim() + "',yxbj_class=" + "'" + textBox5.Text.Trim() + "' Where yxbj_id =" + "'" + textBox1.Text.Trim() + "'";
constring(updates);
}//修改
private void button3_Click(object sender, EventArgs e)
{
string del = "delete from yxbj where yxbj_id =" + "'" + textBox1.Text.Trim() + "'";
constring(del);
}//删除
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int x = e.RowIndex;
textBox1.Text = dt.Rows[x][0].ToString();
textBox2.Text = dt.Rows[x][1].ToString();
textBox3.Text = dt.Rows[x][2].ToString();
textBox4.Text = dt.Rows[x][3].ToString();
textBox5.Text = dt.Rows[x][4].ToString();
}
}
}
展开全部
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=实训.mdb";
OleDbConnection dbConn = new OleDbConnection(conn);
dbConn.Open();
string sql = "select count(*) from 用户表 where LoginName='" + textBox1.Text + "' and PassWord='" + textBox2.Text + "'";
OleDbCommand rd = new OleDbCommand(sql, dbConn);
count = Convert.ToInt32(rd.ExecuteScalar());
if (count == 0)
{
MessageBox.Show("该用户不存在或密码不正确");
}
else
{
查询 chaxun = new 查询();
chaxun.Show();
}
OleDbConnection dbConn = new OleDbConnection(conn);
dbConn.Open();
string sql = "select count(*) from 用户表 where LoginName='" + textBox1.Text + "' and PassWord='" + textBox2.Text + "'";
OleDbCommand rd = new OleDbCommand(sql, dbConn);
count = Convert.ToInt32(rd.ExecuteScalar());
if (count == 0)
{
MessageBox.Show("该用户不存在或密码不正确");
}
else
{
查询 chaxun = new 查询();
chaxun.Show();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询