为什么我创建数据表的时候是成功的 数据库里也显示出数据表来了 也能读出来数据表来可是等我程序下次再运行的时候 数据库里的数据表就消失不见了 以下是我创建数据表的程序:
try
{ string time = DateTime.Now.ToLongDateString(); string SqlCreeate = "CREATE TABLE " + toolStripComboBox1.Text +/* + time + */"(时间 TEXT,工作内容 TEXT,备注 TEXT)"; OleDbCommand OleCmm = new OleDbCommand(SqlCreeate, AcConn); OleCmm.Connection.Open();//打开数据库连接 OleCmm.ExecuteNonQuery(); //执行SQL语句创建新表
AcConn.Close();// 关闭数据源
MessageBox.Show("创建成功");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{ string time = DateTime.Now.ToLongDateString(); string SqlCreeate = "CREATE TABLE " + toolStripComboBox1.Text +/* + time + */"(时间 TEXT,工作内容 TEXT,备注 TEXT)"; OleDbCommand OleCmm = new OleDbCommand(SqlCreeate, AcConn); OleCmm.Connection.Open();//打开数据库连接 OleCmm.ExecuteNonQuery(); //执行SQL语句创建新表
AcConn.Close();// 关闭数据源
MessageBox.Show("创建成功");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.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 System.Runtime;namespace EE
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable(); //适配器
private OleDbDataAdapter ODA = new OleDbDataAdapter(); //数据库连接或更新数据库
private OleDbCommand ACD; //用于SQL语句执行或存储
private BindingSource m_BindingSource = new BindingSource(); //指定数据相关控件和数据源之间的绑定,提供了定位、排序、筛选和更新等方法
//private DataSet AA = new DataSet();
private OleDbConnection AcConn;
public Form1()
{
InitializeComponent();
AcConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=工作日程安排表.mdb;");
}
private void Form1_Load(object sender, EventArgs e)
{
} private void toolStripButton2_Click(object sender, EventArgs e)
{
DT.Rows.Clear();//清空适配器所有行
string a = "select * from " + toolStripComboBox2.Text; //所许打开的数据表名 ACD = new OleDbCommand(a, AcConn); //存储SQL表名与路径
ODA.SelectCommand = ACD; //获取SQL语句
ODA.Fill(DT);// 返回表的记录集给DT
// dataGridView1.DataSource = DT; // 表的记录集给dataGridView用来显示的数据源
m_BindingSource.DataSource = DT; //绑定DT表的记录集
dataGridView1.AutoGenerateColumns = true;
this.bindingNavigator1.BindingSource = m_BindingSource; //数据源绑定导航
this.dataGridView1.DataSource = m_BindingSource; //数据源绑定dataGridView
} private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
try
{
OleDbCommandBuilder SCB = new OleDbCommandBuilder(ODA); //更改与关联数据库的表单命令
this.ODA.Update(DT); //DT记录集更新数据库
MessageBox.Show("更新成功!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{ string time = DateTime.Now.ToLongDateString(); string SqlCreeate = "CREATE TABLE " + toolStripComboBox1.Text +/* + time + */"(时间 TEXT,工作内容 TEXT,备注 TEXT)"; OleDbCommand OleCmm = new OleDbCommand(SqlCreeate, AcConn); OleCmm.Connection.Open();//打开数据库连接 OleCmm.ExecuteNonQuery(); //执行SQL语句创建新表
// OleCmm.Dispose(); //释放
AcConn.Close();// 关闭数据源
MessageBox.Show("创建成功");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}