public void AddRoleMember(string databaseUserName) { using (SqlConnection sqlConnection = new SqlConnection(DevelopmentStorageDbDataContext.GetConnectionString())) { sqlConnection.Open(); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] databaseName = new object[] { DevelopmentStorageDbDataContext.DatabaseName, "db_datareader", databaseUserName }; using (SqlCommand sqlCommand = new SqlCommand(string.Format(invariantCulture, "USE {0} EXEC sp_addrolemember N'{1}', N'{2}'", databaseName), sqlConnection)) { sqlCommand.ExecuteNonQuery(); CultureInfo cultureInfo = CultureInfo.InvariantCulture; object[] objArray = new object[] { DevelopmentStorageDbDataContext.DatabaseName, "db_datawriter", databaseUserName }; sqlCommand.CommandText = string.Format(cultureInfo, "USE {0} EXEC sp_addrolemember N'{1}', N'{2}'", objArray); sqlCommand.ExecuteNonQuery(); } } }
public string GrantDbAccess(string user) { string str; string empty = string.Empty; using (SqlConnection sqlConnection = new SqlConnection(DevelopmentStorageDbDataContext.GetConnectionString())) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(string.Format("USE {0} EXEC sp_helplogins N'{1}'", DevelopmentStorageDbDataContext.DatabaseName, user), sqlConnection)) { SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); DataSet dataSet = new DataSet(); sqlDataAdapter.Fill(dataSet); foreach (DataRow row in dataSet.Tables[1].Rows) { if (!(row.Field <string>("DBName").Trim() == Constants.EmulatorDBName) || !(row.Field <string>("UserOrAlias").Trim() == "User")) { continue; } empty = row.Field <string>("UserName").Trim(); } } if (string.IsNullOrEmpty(empty)) { char[] chrArray = new char[] { '\\' }; string[] strArrays = user.Split(chrArray, 2); empty = ((int)strArrays.Length != 2 ? user : strArrays[1]); using (SqlCommand sqlCommand1 = new SqlCommand(string.Format("USE {0} EXEC sp_grantdbaccess N'{1}', N'{2}'", DevelopmentStorageDbDataContext.DatabaseName, user, empty), sqlConnection)) { sqlCommand1.ExecuteNonQuery(); } } str = empty; } return(str); }