创建SQLite数据库

2015-04-03 15:04:32  访问(1653) 赞(0) 踩(0)


        /// <summary>
        /// 创建SQLite数据库
        /// </summary>
        protected void CreateDB()
        {
            string folderName = comboBox_生成目录.Text.Trim();

            if (folderName.Length == 0)
            {
                MessageBox.Show("请选择生成目录。");
                comboBox_生成目录.Select();
                comboBox_生成目录.Focus();
                return;
            }

            string fileName = comboBox_文件名.Text.Trim();

            if (fileName.Length == 0)
            {
                MessageBox.Show("请选择文件名。");
                comboBox_文件名.Select();
                comboBox_文件名.Focus();
                return;
            }

            string thePwd = comboBox_密码.Text.Trim();

            if (!Directory.Exists(folderName))
                Directory.CreateDirectory(folderName);

            string datasource = "";

            if (folderName.EndsWith("\\") || folderName.EndsWith("/"))
                datasource = folderName + fileName;
            else
                datasource = folderName + "\\" + fileName;

            // FileSlowXFunctions.CreateDirByFileName(datasource);

            System.Data.SQLite.SQLiteConnection.CreateFile(datasource);

            //连接数据库
            System.Data.SQLite.SQLiteConnection conn
                =
                new System.Data.SQLite.SQLiteConnection();

            System.Data.SQLite.SQLiteConnectionStringBuilder connstr 
                = 
                new System.Data.SQLite.SQLiteConnectionStringBuilder();

            connstr.DataSource = datasource;

            if (thePwd.Length > 0)
                connstr.Password = thePwd;//设置密码SQLite ADO.NET实现了数据库密码保护

            conn.ConnectionString = connstr.ToString();
            conn.Open();
            ////创建表
            //System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
            //string sql = "CREATE TABLE test(username varchar(20),password varchar(20))";
            //cmd.CommandText = sql;
            //cmd.Connection = conn;
            //cmd.ExecuteNonQuery();
            ////插入数据
            //sql = "INSERT INTO test VALUES('a','b')";
            //cmd.CommandText = sql;
            //cmd.ExecuteNonQuery();
            ////取出数据
            //sql = "SELECT * FROM test";
            //cmd.CommandText = sql;
            //System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
            //StringBuilder sb = new StringBuilder();


            //while (reader.Read())
            //{
            //    sb.Append("username:").Append(reader.GetString(0)).Append("\n")
            //    .Append("password:").Append(reader.GetString(1));
            //}
            conn.Close();
            conn.Dispose();

            MessageBoxShowYesNoCancel(datasource, folderName);
        }


        /// <summary>
        /// 保存成功后的提示操作
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="dirName"></param>
        public void MessageBoxShowYesNoCancel(string fileName, string dirName)
        {
            DialogResult dg = MessageBox.Show(
                       "保存成功,是否复制文件或打开目录(点No)?", "选择操作", MessageBoxButtons.YesNoCancel);

            if (dg == DialogResult.Cancel)
                return;

            if (dg == DialogResult.No)
            {
                System.Diagnostics.Process.Start("explorer.exe", dirName);

                return;
            }

            if (dg == DialogResult.Yes)
            {

                System.Collections.Specialized.StringCollection stringCollectionValue
                    = new System.Collections.Specialized.StringCollection();

                stringCollectionValue.Add(fileName);

                Clipboard.SetFileDropList(stringCollectionValue);
            }
        }


标签:创建SQLite数据库 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)