Creating a database instance in C#

雨燕双飞 提交于 2021-02-05 04:55:41

问题


Is it possible to create a sql database instance with C# code. I have no problem adding a database to an existing SQL instance, for example WONEA\SQLEXPRESS but for creating another SQL instance such as WONEA\SQLEXPRESSTEST I'm a little stumped. Help!


回答1:


The short answer is you can't do this using C# code.

SQL server instances are essentially installations of SQL server - in order to create a new SQL server instance you need to run the SQL Server installer, and ask it to install one (which you can technically do from C# code, but I gather thats not the answer you were looking for).




回答2:


Creating new instances requires running the installer for SQL server, so I would imagine the answer is "No." (Would be interested to hear otherwise though!)




回答3:


You could use a command prompt process to create a SQL instance if you have SQL Local DB installed. See code example below:

Process proc = new Process();
ProcessStartInfo info = new ProcessStartInfo
{
     FileName = "cmd.exe",
     Arguments = $"/C {argument}",
     RedirectStandardOutput = true,
     UseShellExecute = false,
     CreateNoWindow = true
};

proc.StartInfo = info;
proc.Start();

Where Argument is a string parameter that looks something like this:

sqllocaldb create "SqlInstance"

This instruction will create a new SQL instance called SqlInstance.



来源:https://stackoverflow.com/questions/3036543/creating-a-database-instance-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!