主题:关于c#向access数据库插入数据[求助]
找了半天找不到...请各位大侠帮偶看看....谢谢了....
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 xiugaimdb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection mycn;
OleDbCommand oledbcmd;
private void Form1_Load(object sender, EventArgs e)
{
mycn = new OleDbConnection();
mycn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "xsxx.mdb";
oledbcmd = new OleDbCommand();
oledbcmd.Connection = mycn;
oledbcmd.CommandType = CommandType.Text;
oledbcmd.CommandText = "select * from [xsxx]";
mycn.Open();
OleDbDataReader dReader = oledbcmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dReader.Read())
{
ListViewItem newItem=listView1.Items.Add(dReader["xs_xh"].ToString().Trim());
newItem.SubItems.Add(dReader["xs_xm"].ToString().Trim());
newItem.SubItems.Add(dReader["xs_xb"].ToString().Trim());
newItem.SubItems.Add(dReader["xs_dz"].ToString().Trim());
newItem.SubItems.Add(dReader["xs_cj"].ToString().Trim());
}
dReader.Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
textBox1.Text = listView1.SelectedItems[0].Text;
textBox2.Text = listView1.SelectedItems[0].SubItems[1].Text;
textBox3.Text = listView1.SelectedItems[0].SubItems[2].Text;
textBox4.Text = listView1.SelectedItems[0].SubItems[3].Text;
textBox5.Text = listView1.SelectedItems[0].SubItems[4].Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "添加")
{
button1.Text = "保存";
textBox1.Text = ""; textBox2.Text = "";
textBox3.Text = ""; textBox4.Text = "";
textBox5.Text = ""; textBox1.Focus();
textBox1.ReadOnly = false; textBox2.ReadOnly = false;
textBox3.ReadOnly = false; textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
button2.Enabled = false; button3.Enabled = false;
}
else
{
oledbcmd.CommandText = "INSERT INTO [xsxx] (xs_xh,xs_xm,xs_xb,xs_dz,xs_cj) VALUES('"
+textBox1.Text+"','"
+textBox2.Text+"','"
+textBox3.Text+"','"
+textBox4.Text+"','"
+textBox5.Text+"')";
mycn.Open();
int cmdresults = oledbcmd.ExecuteNonQuery();
mycn.Close();
if (cmdresults == 1)
{
textBox1.ReadOnly = true; textBox2.ReadOnly = true;
textBox3.ReadOnly = true; textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
button2.Enabled = true; button3.Enabled = true;
ListViewItem newItem = listView1.Items.Add(textBox1.Text);
newItem.SubItems.Add(textBox2.Text);
newItem.SubItems.Add(textBox3.Text);
newItem.SubItems.Add(textBox4.Text);
newItem.SubItems.Add(textBox5.Text);
button1.Text = "添加";
MessageBox.Show("插入数据成功!");
}
else
MessageBox.Show("插入数据失败!");
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
if (button2.Text == "修改")
{
button2.Text = "保存";
textBox2.ReadOnly = false; textBox3.ReadOnly = false;
textBox4.ReadOnly = false; textBox5.ReadOnly = false;
textBox2.Enabled = false; textBox3.Enabled = false;
textBox2.Focus();
}
else
{
string xs_xh = listView1.SelectedItems[0].Text;
oledbcmd.CommandText = "update [xsxx] set xs_xm='" + textBox2.Text + "',xs_xb='" + textBox3.Text + "',xs_dz='" + textBox4.Text + "',xs_cj='" + textBox5.Text + "'where xs_xh='" + xs_xh + "'";
mycn.Open();
int cmdresults = oledbcmd.ExecuteNonQuery();
mycn.Close();
if (cmdresults == 1)
{
textBox2.ReadOnly = true; textBox2.ReadOnly = true;
textBox4.ReadOnly = true; textBox5.ReadOnly = true;
button2.Enabled = true; textBox5.ReadOnly = true;
listView1.SelectedItems[0].SubItems[1].Text = textBox2.Text;
listView1.SelectedItems[0].SubItems[2].Text = textBox3.Text;
listView1.SelectedItems[0].SubItems[3].Text = textBox4.Text;
listView1.SelectedItems[0].SubItems[4].Text = textBox5.Text;
button2.Text = "修改";
MessageBox.Show("修改数据成功!");
}
else
MessageBox.Show("修改数据失败!");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string xs_xh = listView1.SelectedItems[0].Text;
oledbcmd.CommandText = "delete from [xsxx] where xs_xh='" + xs_xh + "'";
mycn.Open();
oledbcmd.ExecuteNonQuery();
mycn.Close();
listView1.Items.Remove(listView1.SelectedItems[0]);
textBox1.Text = ""; textBox2.Text = "";
textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = "";
}
}
}
}
插入代码在button1_Click事件中... 还是我别的什么地方出了错...中是郁闷...
大家帮偶看看...谢谢了...