日期:2014-05-17 浏览次数:20741 次
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.Data.SqlServerCe;
using System.IO;
namespace ReadSDFFileOnDesktop
{
public partial class MainForm : Form
{
public static string strSQLPath = "DB.sdf";
public static string strConString = "Data Source = " + strSQLPath + "; Password = ";
public static string strAccessString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Pub.mdb;";
public MainForm()
{
InitializeComponent();
}
private void buttonCreateDB_Click(object sender, EventArgs e)
{
// 创建数据库
if (File.Exists(strSQLPath))
{
// 删除数据库
File.Delete(strSQLPath);
CreateDataBase();
}
else
{
// 创建数据库
CreateDataBase();
}
}
private void CreateDataBase()
{
// 创建数据库文件
SqlCeEngine engine = new SqlCeEngine(strConString);
engine.CreateDatabase();
engine.Dispose();
MessageBox.Show("数据库创建成功!");
}
private void buttonCreateTable_Click(object sender, EventArgs e)
{
if (File.Exists(strSQLPath))
{
// 创建表
SqlCeConnection cn = new SqlCeConnection(strConString);
cn.Open();
try
{
string strSQL = @"CREATE TABLE employee(emp_id nvarchar(9),
fname nvarchar(20),minit nvarchar(1), lname nvarchar(30),
job_id nvarchar(8),job_lvl nvarchar(8), pub_id nvarchar(4),
hire_date datetime)";
SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL, cn);
SqlCeCmd.ExecuteNonQuery();
MessageBox.Show("数据库中的表创建成功", "提示信息");
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
}
else
{
MessageBox.Show("请先创建数据库!");
}
}