private void okButton_Click(object sender, System.EventArgs args) { account = accountBox.Text.Trim(); if (account.Length == 0) { MessageBox.Show("Please enter an account name.", wizard.SetupTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); accountBox.Focus(); accountBox.SelectAll(); return; } password = passwordBox.Text.Trim(); if (password != confirmBox.Text.Trim()) { MessageBox.Show("The password and confirmation are not the same.", wizard.SetupTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); passwordBox.Text = string.Empty; confirmBox.Text = string.Empty; passwordBox.Focus(); passwordBox.SelectAll(); return; } // Create the account SqlConnectionInfo conInfo; SqlContext ctx; SqlCommand cmd; WaitForm waitForm; wizard.Enabled = false; this.Update(); waitForm = new WaitForm("Creating account [" + account + "]..."); waitForm.TopLevel = true; waitForm.Show(); waitForm.Update(); Thread.Sleep(2000); conInfo = new SqlConnectionInfo((string)wizard.SetupState["connectionString"]); conInfo.Database = "master"; ctx = new SqlContext(conInfo.ToString()); try { ctx.Open(); if (ctx.IsSqlAzure) { cmd = ctx.CreateCommand("create login {0} with password='******'", account, password); } else { cmd = ctx.CreateSPCall("sp_addlogin"); cmd.Parameters.Add("@loginame", SqlDbType.VarChar).Value = account; cmd.Parameters.Add("@passwd", SqlDbType.VarChar).Value = password; } ctx.Execute(cmd); } catch (Exception e) { passwordBox.Text = string.Empty; confirmBox.Text = string.Empty; wizard.Enabled = true; waitForm.Close(); MessageBox.Show("Cannot create the new database account.\r\n\r\n" + e.Message, wizard.SetupTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { ctx.Close(); } wizard.Enabled = true; waitForm.Close(); // Success! DialogResult = DialogResult.OK; }
private void LoadAccounts() { // Load the listbox with the list of existing server logins, filtering // out the SA, Windows, or server role accounts as well as any built-in // accounts that begin with "##". SqlConnectionInfo conInfo; SqlContext ctx; SqlCommand cmd; DataSet ds; DataTable dt; WaitForm waitForm; string login; wizard.Enabled = false; this.Update(); waitForm = new WaitForm("Scanning accounts..."); waitForm.TopLevel = true; waitForm.Show(); waitForm.Update(); Thread.Sleep(2000); // Connect to the master database. conInfo = new SqlConnectionInfo(conString); conInfo.Database = "master"; ctx = new SqlContext(conInfo.ToString()); try { ctx.Open(); // Get the accounts (note that the sp_helplogins sproc does not exist on SQL Azure). if (ctx.IsSqlAzure) { cmd = ctx.CreateCommand("select name as 'LoginName' from sys.sql_logins"); } else { cmd = ctx.CreateSPCall("sp_helplogins"); } ds = ctx.ExecuteSet(cmd); dt = ds.Tables["0"]; accountList.Items.Clear(); foreach (DataRow row in dt.Rows) { login = SqlHelper.AsString(row["LoginName"]); // Append the account, skipping any that are empty or // appear to be a server role, a Windows domain account, // or a built-in account. if (login == null) { continue; // Empty } if (login.IndexOf('\\') != -1) { continue; // Windows account } if (String.Compare(login, "sa", true) == 0) { continue; // SA account } if (login.StartsWith("##")) { continue; // Built-in account } accountList.Items.Add(login); if (String.Compare(login, (string)wizard.SetupState["account"], true) == 0) { accountList.SelectedIndex = accountList.Items.Count - 1; } } } catch (Exception e) { wizard.Enabled = true; waitForm.Close(); MessageBox.Show("Cannot load database accounts.\r\n\r\n" + e.Message, wizard.SetupTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { ctx.Close(); } wizard.Enabled = true; waitForm.Close(); }
public void SqlConnectionInfo_Basic() { SqlConnectionInfo info; ArgCollection args; info = SqlConnectionInfo.Parse("server=foo"); Assert.AreEqual("foo", info.Server); info = SqlConnectionInfo.Parse("SERVER=foo"); Assert.AreEqual("foo", info.Server); info = SqlConnectionInfo.Parse("data source=foo"); Assert.AreEqual("foo", info.Server); info = SqlConnectionInfo.Parse("DATA SOURCE=foo"); Assert.AreEqual("foo", info.Server); info = SqlConnectionInfo.Parse("initial catalog=foo"); Assert.AreEqual("foo", info.Database); info = SqlConnectionInfo.Parse("INITIAL CATALOG=foo"); Assert.AreEqual("foo", info.Database); info = SqlConnectionInfo.Parse("database=foo"); Assert.AreEqual("foo", info.Database); info = SqlConnectionInfo.Parse("DATABASE=foo"); Assert.AreEqual("foo", info.Database); info = SqlConnectionInfo.Parse("uid=foo"); Assert.AreEqual("foo", info.UserID); info = SqlConnectionInfo.Parse("UID=foo"); Assert.AreEqual("foo", info.UserID); info = SqlConnectionInfo.Parse("user id=foo"); Assert.AreEqual("foo", info.UserID); info = SqlConnectionInfo.Parse("USER ID=foo"); Assert.AreEqual("foo", info.UserID); info = SqlConnectionInfo.Parse("pwd=foo"); Assert.AreEqual("foo", info.Password); info = SqlConnectionInfo.Parse("PWD=foo"); Assert.AreEqual("foo", info.Password); info = SqlConnectionInfo.Parse("password=foo"); Assert.AreEqual("foo", info.Password); info = SqlConnectionInfo.Parse("PASSWORD=foo"); Assert.AreEqual("foo", info.Password); info = SqlConnectionInfo.Parse("integrated security=SSPI"); Assert.AreEqual("SSPI", info.Security); info = SqlConnectionInfo.Parse("INTEGRATED security=SSPI"); Assert.AreEqual("SSPI", info.Security); info = SqlConnectionInfo.Parse("Trusted_Connection=true"); Assert.AreEqual("SSPI", info.Security); info = SqlConnectionInfo.Parse("TRUSTED_Connection=TRUE"); Assert.AreEqual("SSPI", info.Security); info = SqlConnectionInfo.Parse("Trusted_Connection=false"); Assert.IsNull(info.Security); info = SqlConnectionInfo.Parse("Other=foo"); Assert.AreEqual("foo", info["OTHER"]); info = new SqlConnectionInfo("server=myserver;database=mydatabase;uid=myuser;pwd=mypassword;other=foo"); Assert.AreEqual("myserver", info.Server); Assert.AreEqual("mydatabase", info.Database); Assert.AreEqual("myuser", info.UserID); Assert.AreEqual("mypassword", info.Password); Assert.AreEqual("foo", info["other"]); args = new ArgCollection(info.ToString()); Assert.AreEqual("myserver", args["server"]); Assert.AreEqual("mydatabase", args["database"]); Assert.AreEqual("myuser", args["uid"]); Assert.AreEqual("mypassword", args["pwd"]); Assert.IsNull(args["integrated security"]); info = SqlConnectionInfo.Parse("server=foo"); Assert.IsNull(info["bar"]); info = new SqlConnectionInfo(); info.Server = "myserver"; info.Database = "mydatabase"; info.UserID = "myid"; info.Password = "******"; info.Security = "SSPI"; Assert.AreEqual("myserver", info.Server); Assert.AreEqual("mydatabase", info.Database); Assert.AreEqual("myid", info.UserID); Assert.AreEqual("mypassword", info.Password); Assert.AreEqual("SSPI", info.Security); args = new ArgCollection(info.ToString()); Assert.AreEqual("myserver", args["server"]); Assert.AreEqual("mydatabase", args["database"]); Assert.AreEqual("myid", args["uid"]); Assert.AreEqual("mypassword", args["pwd"]); Assert.AreEqual("SSPI", args["integrated security"]); info = new SqlConnectionInfo("server=foo;database=bar"); args = new ArgCollection(info.ToString()); Assert.AreEqual("foo", args["server"]); Assert.AreEqual("bar", args["database"]); Assert.AreEqual("SSPI", args["integrated security"]); }