This is not a problem, when you have SQL Server Management studio installed. But what if you don’t have Management Studio is not installed? In this case you can use sqlcmd utility. In some cases, you need to backup databases programmatically and this could be done via Server Management objects. In this short example I will show you how to create SQL Server login and assign it to sysadmin server role.
ServerConnection conn = new ServerConnection("SQLSERVERINSTANCE", "USERNAME", "PASSWORD"); try { Server srv = new Server(conn); Login newLogin = new Login(srv, "TestLogin"); newLogin.LoginType = Microsoft.SqlServer.Management.Smo.LoginType.SqlLogin; newLogin.Create("TestPassword"); newLogin.AddToRole("sysadmin"); } catch (Exception err) { Console.WriteLine(err.Message); }This short example connects to SQL Server instance and creates new login called TestLogin with sysadmin server role.
No comments:
Post a Comment