熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

WinCE下對數據庫的操作方法實例

2022-06-13   來源: .NET編程 

  一起和新手朋友學習一下WinCE下對數據庫的操作方法

  using System;
        using SystemCollectionsGeneric;
        using SystemComponentModel;
        using SystemData;
        using SystemDrawing;
        using SystemText;
        using SystemWindowsForms;
        using SystemDataOleDb;
        using SystemDataSqlServerCe;
        using SystemIO;
        namespace ReadSDFFileOnDesktop
        {
        public partial class MainForm : Form
          {
            public static string strSQLPath = DBsdf;
            public static string strConString = Data Source = + strSQLPath + ; Password = ;
            public static string strAccessString = Provider=MicrosoftJetOLEDB;Data Source=Pubmdb;;
            public MainForm()
            {
                InitializeComponent();
            }
            private void buttonCreateDB_Click(object sender EventArgs e)
            {
                // 創建數據庫
                if (FileExists(strSQLPath))
                {
                    // 刪除數據庫
                    FileDelete(strSQLPath);
                    CreateDataBase();
                }
                else
                {
                    // 創建數據庫
                    CreateDataBase();
                }
            }
            private void CreateDataBase()
            {
                // 創建數據庫文件
                SqlCeEngine engine = new SqlCeEngine(strConString);
                engineCreateDatabase();
                engineDispose();
                MessageBoxShow(數據庫創建成功!);
            }
            private void buttonCreateTable_Click(object sender EventArgs e)
            {
                if (FileExists(strSQLPath))
                {
                    // 創建表
                    SqlCeConnection cn = new SqlCeConnection(strConString);
                    cnOpen();
                    try
                    {
                        string strSQL = @CREATE TABLE employee(emp_id nvarchar()
                        fname nvarchar()minit nvarchar() lname nvarchar()
                        job_id nvarchar()job_lvl nvarchar() pub_id nvarchar()
                        hire_date datetime);
                        SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL cn);
                        SqlCeCmdExecuteNonQuery();
                        MessageBoxShow(數據庫中的表創建成功 提示信息);
                    }
                    catch (SqlCeException ex)
                    {
                        MessageBoxShow(exMessage);
                    }
                    finally
                    {
                        cnClose();
                    }
                }
                else
                {
                    MessageBoxShow(請先創建數據庫!);
                }
            }
            private void buttonImportData_Click(object sender EventArgs e)
            {
                // 導入數據
                OleDbConnection OleDbCn = new OleDbConnection(strAccessString);
                try
                {
                    OleDbCnOpen();
                    OleDbCommand OleDbCmd = new OleDbCommand(SELECT * FROM employee OleDbCn);
                    OleDbDataReader OleDbDr = OleDbCmdExecuteReader();
                    SqlCeConnection SqlCeCn = new SqlCeConnection(strConString);
                    try
                    {
                        SqlCeCnOpen();
                        SqlCeCommand SqlCeCmd = new SqlCeCommand();
                        SqlCeCmdConnection = SqlCeCn;
                        while (OleDbDrRead())
                        {
                            string strSQL = ;
                            strSQL = StringFormat(INSERT INTO employee +
                            (emp_id fname minit lname job_id job_lvl pub_id hire_date)
                            VALUES ({} {} {} {} {} {} {} {})
                            OleDbDrGetValue(OleDbDrGetOrdinal(emp_id))
                            OleDbDrGetValue(OleDbDrGetOrdinal(fname))
                            OleDbDrGetValue(OleDbDrGetOrdinal(minit))
                            OleDbDrGetValue(OleDbDrGetOrdinal(lname))
                            OleDbDrGetValue(OleDbDrGetOrdinal(job_id))
                            OleDbDrGetValue(OleDbDrGetOrdinal(job_lvl))
                            OleDbDrGetValue(OleDbDrGetOrdinal(pub_id))
                            OleDbDrGetValue(OleDbDrGetOrdinal(hire_date)));
                            SqlCeCmdCommandText = strSQL;
                            SqlCeCmdExecuteNonQuery();
                        }
                        MessageBoxShow(數據導入成功!);
                    }
                    catch (SqlCeException ex)
                    {
                        MessageBoxShow(exMessage);
                    }
                    finally
                    {
                        SqlCeCnClose();
                    }
                }
                catch (OleDbException ex)
                {
                    MessageBoxShow(exMessage);
                }
                finally
                {
                    OleDbCnClose();
                }
            }
            private void buttonShowData_Click(object sender EventArgs e)
            {
                // 顯示數據
                SqlCeConnection SqlCeCn = new SqlCeConnection(strConString);
                try
                {
                    SqlCeCnOpen();
                    string strSQL = ;
                    strSQL = StringFormat(SELECT * FROM employee );
                    SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL SqlCeCn);
                    SqlCeDataReader SqlCeDr = SqlCeCmdExecuteReader();
                    listViewItemsClear();
                    while (SqlCeDrRead())
                    {
                        ListViewItem item;
                        item = new ListViewItem(SqlCeDrGetValue
                        (SqlCeDrGetOrdinal(emp_id))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(fname))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(minit))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(lname))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(job_id))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(job_lvl))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(pub_id))ToString());
                        itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
                        GetOrdinal(hire_date))ToString());
                        listViewItemsAdd(item);
                    }
                }
                catch (SqlCeException ex)
                {
                    MessageBoxShow(exMessage);
                }
                finally
                {
                    SqlCeCnClose();
                }
            }
          }
        }


From:http://tw.wingwit.com/Article/program/net/201311/13452.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.