본문 바로가기
[개발] 이야기/[DotNet] 이야기

xamarin local project db file import used

by 헤이나우
반응형

How to import the db file in xamarin to the project and copy the file to Android when starting the program and use it

Import Sqlite db file

Xamarin => xxx.Android Project =>MainActivity.cs => OnCreate Method => LoadApplication(new App) Bottom Add.

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string dbPath = Path.Combine(path, "test.db");
CopoyToDevice(dbPath);

Copy Method!

/// <summary>
        /// Copy To Device sqlite db File
        /// </summary>
        /// <param name="dbPath">Local Path</param>
        private void CopoyToDevice(string dbPath)
        {
            bool flag = true;
#if DEBUG == false
            flag = !File.Exists(dbPath)
#endif

            if (flag)
            {
                using (var br = new BinaryReader(Application.Context.Assets.Open("test.db")))
                {
                    using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
                    {
                        byte[] buffer = new byte[2048];
                        int length = 0;
                        while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            bw.Write(buffer, 0, length);
                        }
                    }
                }
            }
        }

In debug mode, copying is always attempted, and in release mode, when there is no file, copying is attempted only once.

 

Used!

반응형

댓글