Database already exist. Choose a Different Name using CreateDatabase()

前端 未结 4 946
走了就别回头了
走了就别回头了 2020-12-15 11:47

I got an issue and learned something at the same time....

I created a DBML from an existing server database.

From the DBML I wanted to create local database

4条回答
  •  攒了一身酷
    2020-12-15 12:21

    A solution (via here) is to use SSEUtil to detach the existing db:

    try
    {
        // open a connection to the database for test
    }
    catch (SystemException ex) // Change exception type based on your underlying data provider
    {
        if (ex.Message.ToLower().Contains("already exists. choose a different database name"))
        {
            var match = Regex.Match(ex.Message, "database '(.*)' already exists.", 
               RegexOptions.IgnoreCase);
    
            if (match.Success)
            {
                String dbFileName = match.Groups[1].Value;
                Process p = new Process();
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.FileName = String.Format("{0}/Tools/SSEUtil.exe", 
                  Environment.CurrentDirectory);
                p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                p.StartInfo.Arguments = String.Format("-d \"{0}\"", dbFileName);
    
                p.Start();
            }
        }
    }
    

提交回复
热议问题