/// <summary> /// Check whether the selected database is empty /// </summary> /// <param name="connectionString"></param> /// <returns></returns> private bool IsEmptyDatabase(string connectionString) { bool isEmptyDatabase; DataTable dt; string emptyDBCheckingSql = "select name from sysobjects where xtype = 'U' and status>=0"; dt = SqlTool.ExecuteDataTable(connectionString, emptyDBCheckingSql); isEmptyDatabase = (dt.Rows.Count == 0); return(isEmptyDatabase); }
/// <summary> /// Check whether the form data are valid /// 1. If web SQL login is NT User /// and need to be granted access permission /// 1.1 NT user name is required /// 1.2 This user must be SQL server login /// 2. If the login is SQL user /// 2.1 User name and password are required /// 2.2 This login must be valid /// </summary> /// <returns></returns> private bool SaveState() { SqlUserType userType; string ntUserName; string userName, password; string connectionString; bool grantPermission; DataTable dt; string ntUserCheckingSql = "select * from master.dbo.syslogins where name='{0}'"; bool isValid; //Get Form data if (this.rdoWindowsUser.Checked) { userType = SqlUserType.IntegratedUser; } else { userType = SqlUserType.SQLUser; } ntUserName = this.txtNTUser.Text; userName = this.txtUsername.Text; password = this.txtPassword.Text; grantPermission = this.rdoGrantPermissionYes.Checked; isValid = true; // 1. If web SQL login is NT User // and need to be granted access permission // 1.1 NT user name is required // 1.2 This user must be SQL server login if (userType == SqlUserType.IntegratedUser && grantPermission) { connectionString = SqlTool.GetConnectionString(ApplicationState.ServerName, "Master", ApplicationState.DboUserType, ApplicationState.DboUsername, ApplicationState.DboPassword); try { dt = SqlTool.ExecuteDataTable(connectionString, string.Format(ntUserCheckingSql, ntUserName.Replace("'", "''"))); if (dt.Rows.Count == 0) { MessageBox.Show("The selected NT login doesn't exist in the SQL server " + ApplicationState.ServerName, "Invalid Login", MessageBoxButtons.OK); isValid = false; } else { isValid = true; } } catch (Exception exp) { MessageBox.Show("Connection failed.\n" + exp.Message); isValid = false; } } // 2. If the login is SQL user // 2.1 User name and password are required // 2.2 This login must be valid if (userType == SqlUserType.SQLUser) { connectionString = SqlTool.GetConnectionString(ApplicationState.ServerName, "Master", userType, userName, password); isValid = SqlTool.CheckConnection(connectionString); } //If the data are valid, save data if (isValid) { ApplicationState.WebUserType = userType; ApplicationState.WebNTUsername = ntUserName; ApplicationState.WebUsername = userName; ApplicationState.WebPassword = password; ApplicationState.WebGrantPermission = grantPermission; } return(isValid); } //end of checking