开发者

how to deploy database

开发者 https://www.devze.com 2023-02-22 09:23 出处:网络
Hi what is the best way to deploy database script to my client pc? i have installed the app and have the sql management studio installed at my client pc. Everything went smooth except whe开发者_运维知

Hi what is the best way to deploy database script to my client pc? i have installed the app and have the sql management studio installed at my client pc. Everything went smooth except whe开发者_运维知识库n i need to update the database. How do i update the database without having me to go to their pc to run the .sql update script. This question applies when my app is used at other country.

For updating code is fine as i am using the ClickOnce. This is a window based app.

Please help


I would ship the SQL file with your application, and have application startup logic that always checks a certain location for scripts that need to be run. It could be as simple as checking a [application path]\MigrationScripts folder for all .sql files. If files are found, read in the contents and execute against the database.

Once the check (and potential database updates) are complete, continue loading the application.

Sample Code (untested), and a very basic implementation:

public class Migration
{
    private string _migrationPath = @"C:\temp\MigrationSteps"; //change
    private string[] _sqlFiles = null;

    public Migration()
    {
        Initialize();
    }

    public Migration(string path)
    {
        _migrationPath = path;
        Initialize();
    }

    private void Initialize()
    {
        _sqlFiles = Directory.GetFiles(_migrationPath, "*.sql");
    }

    public bool Run()
    {
        bool success = true;

        foreach (string sqlFile in _sqlFiles)
        {
            ExecuteRun(File.ReadAllText(sqlFile));
        }

        return success; //Do something with this value
    }

    public bool CleanUp()
    {
        //Put some logic here to "clean up" files that have already been run.
        throw new NotImplementedException();
    }

    private bool ExecuteRun(string sqlText)
    {
        //Call your data access library and execute the sqlText
        throw new NotImplementedException();
    }
}

Usage:

Migration migration = new Migration();
if (migration.Run())
{
    migration.CleanUp();
}
else
{
    //Do something
}
0

精彩评论

暂无评论...
验证码 换一张
取 消