SqlCeConnection myconn = new SqlCeConnection("DataSource=mysdf.sdf");
SqlCeCommand com = new SqlCeCommand("create table PC (PCNO NVarChar(10) primary key,XH NVarChar(10) NOT NULL,WERKS NVarChar(4) NOT NULL,PC_DATE NVarChar(10))");
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.SqlClient; using System.Data.SqlServerCe; using System.Net; using System.IO;
namespace WinAppGrid { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //创建数据库的表 private Boolean CreateTable() { try { SqlCeConnection myconn = new SqlCeConnection("DataSource=mysdf.sdf"); SqlCeCommand com = new SqlCeCommand("create table PC (PCNO NVarChar(10) primary key,XH NVarChar(10) NOT NULL,WERKS NVarChar(4) NOT NULL,PC_DATE NVarChar(10))"); myconn.Open(); com.Connection = myconn; com.ExecuteNonQuery(); myconn.Close(); MessageBox.Show("创建数据库成功!"); return true; } catch (Exception ex) { MessageBox.Show("创建数据库失败" + ex.ToString().Substring(1, 60)); return false; } }
private void button1_Click(object sender, EventArgs e) { //创建数据库 try { if ( File.Exists("mysdf.sdf")) { MessageBox.Show("数据库已经存在"); } else { SqlCeEngine eng = new SqlCeEngine("DataSource=mysdf.sdf"); eng.CreateDatabase(); eng.Dispose(); MessageBox.Show("创建数据库成功"); } Boolean rtu; rtu = CreateTable(); if (rtu = false) { MessageBox.Show("数据表已经存在!");
} else { MessageBox.Show("创建数据表成功!"); } } catch(Exception ex) { MessageBox.Show("创建数据库失败"+ ex.ToString().Substring(1,60)); }
}
} } |