.NET Programming
 

     
Code to Add SQL Server Database Login and User using SMO

Keywords: SMO SQL Server Login user ServerConnection Master SQLClient

 

 
 

serevent generic

serevent inhaler side effects

side effects of alcohol and antidepressants

side effects of mixing alcohol and antidepressants link

generic viagra without a doctor prescription

buy generic viagra online jensen.azurewebsites.net

abortion pill online pharmacy

buy abortion pill online safely blog.keylink.rs

rescue inhaler albuterol

asthma rescue inhaler overuse website

naltrexone implant reviews

naltrexone reviews australia tymejczyk.com

prednisolon kol

prednisolon

cialis generico online

cialis senza ricetta sporturfintl.com

ventolin asthma attack

ventolin asthma inhaler buy online

fluoxetine and alcohol effects

prozac alcohol liver click here

fluticasone furoate

otc inhaler for cough

amitriptyline dosage

amitriptyline hydrochloride 25 mg link

generico cialis farmacia

acquistare cialis in farmacia senza ricetta

 

        using Microsoft.SqlServer.Management.Smo;

        using Microsoft.SqlServer;

        using System.Data.SqlClient;

        using Microsoft.SqlServer.Management.Common;

 

        private void button1_Click(object sender, EventArgs e)

        {

            ServerConnection ServerConn = new ServerConnection();

            OpenConnection (ref ServerConn ,"User","Password","YourServer");

            Server SqlServer = new Server(ServerConn);

            string Error = AddLogin(ref SqlServer, "NewUser", "NewPassword",

"YourDatabase");               

            if (Error.Length  > 0 )

            {

                MessageBox.Show ("Adding Login for  failed: " + Error);

            }

        }

 

        public string AddLogin(ref Server Svr1, string NewLogin, string PassWord, string

DatabaseName)

        {

            bool found = false;

            try

            {

                foreach (Login L in Svr1.Logins)

                {

                    if (NewLogin.ToUpper().Trim() == L.Name.ToUpper().Trim())

                    {

                        found = true;

                    }

                }

                if (found == false)

                {

                    Login nl = new Login(Svr1, NewLogin);

                    if (Svr1.Information.Version.Major > 8)

                    {

                        //Optional logic to disable complexity rules

                        nl.PasswordExpirationEnabled = false;

                        nl.PasswordPolicyEnforced = false;

                    }

                    nl.LoginType = LoginType.SqlLogin;

                    nl.DefaultDatabase = DatabaseName;

                    nl.Create(PassWord);

 

 

                }

            }

            catch (Exception ex)

            {

                return "An error occurred creating the login " + NewLogin +

                    ", this user will not have access to the database: " +

                    ex.Message + (char)13 + (char)10 + ex.InnerException;

            }

 

            try

            {

                bool Found = false;

                Database db = Svr1.Databases[DatabaseName];

 

                foreach (User du in db.Users)

                {

                    if (du.Name == NewLogin)

                        Found = true;

                }

 

                if (Found == false)

                {

                    User U = new User(db, NewLogin);

                    U.Login = NewLogin;

                    U.UserType = UserType.SqlLogin;

                    U.Create();

                    U.AddToRole("db_owner");

                }

 

                // optional code for the Master Database

                string MasterDatabase = "master";

                Database db1 = Svr1.Databases[MasterDatabase];

                Found = false;

                User Um = new User();

                foreach (User du in db1.Users)

                {

                    if (du.Name == NewLogin)

                    {

                        Found = true;

                        Um = du;

 


 PTerryWave LLC      Return to Programming Listing
About