|
附我的代码
- public partial class Form1 : Form
- {
- public static readonly string APPPATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
- //string cesqlconnstr = "";
- public Form1()
- {
- InitializeComponent();
- //cesqlconnstr = "DataSource=testdb.sdf";
- }
- public delegate void dlgShoMsg(string pMsg);
- private void ShowMsg(string pMsg)
- {
- if (this.label1.InvokeRequired)
- {
- dlgShoMsg dlg = new dlgShoMsg(ShowMsg);
- this.label1.Invoke(dlg, new object[] { pMsg });
- }
- else
- {
- this.label1.Text = pMsg;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string sFileName = "testdb.sdf";
- System.IO.File.Delete(APPPATH + "\"+ sFileName);
- string sPaaa = "DataSource=" + APPPATH + "\" + sFileName;
- Db4SqlCE.CreateSDF(sPaaa);
- Db4SqlCE db = new Db4SqlCE(sPaaa);
- string sSql = @"CREATE TABLE [pubgoods] (
- [goodsid] [nvarchar] (50) NOT NULL PRIMARY KEY,
- [styleid] [nvarchar] (50) NULL ,
- [styledesc] [nvarchar] (200) NULL ,
- [sizecategoryid] [nvarchar] (50) NULL ,
- [colorid] [nvarchar] (50) NULL ,
- [colordesc] [nvarchar] (50) NULL ,
- [sizeid] [nvarchar] (50) NULL ,
- [sizedesc] [nvarchar] (50) NULL
- )
- ";
- db.Execute(sSql);
- System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart
- (Doit));
- th.IsBackground = true;
- th.Start();
- }
- private void Doit()
- {
- int iRow = 1000;
- string sFileName = "testdb.sdf";
- string sPaaa = "DataSource=" + APPPATH + "\" + sFileName;
- Db4SqlCE db = new Db4SqlCE(sPaaa);
- db.ConnectionKeepOpenTf = true;
- db.TransactionBegin();
- try
- {
- string sSql = "";
- for (int i = 1; i <= iRow; i++)
- {
- sSql = string.Format(@"INSERT INTO [pubgoods]([goodsid], [styleid], [styledesc],
- [sizecategoryid], [colorid], [colordesc], [sizeid], [sizedesc])
- select '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}'"
- , i.ToString(), "styleid".ToString().Trim()
- , "styledesc".ToString().Trim(), "sizecategoryid".ToString().Trim()
- , "colorid".ToString().Trim(), "colordesc".ToString().Trim()
- , "sizeid".ToString().Trim(), "sizedesc".ToString().Trim());
- db.Execute(sSql,true);
- this.ShowMsg(i.ToString() + "/" + iRow.ToString());
- }
- }
- catch (Exception err)
- {
- db.TransactionRollback();
- MessageBox.Show(err.Message);
- return;
- }
- db.TransactionCommit();
- }
- }
复制代码 |
|