Create SQL SERVER login (windows server 2k8)
-
Wednesday, March 07, 2012 8:46 AM
Using SMO I've tried to add an SQL SERVER login installed in windows 7 with this code and it worked well, but it didn't work on windows server 2008.
I have got this error message : Create failed for Login 'LoginName'
- Am I missing something ?
Login _Login; string _UserID ; string _User; string _Password; try { _Login = new Login(wm._Server, _UserID); _Login.LoginType = LoginType.SqlLogin; _Login.Create(_Password); _Login.AddToRole("sysadmin"); } catch (Exception ex1) { MessageBox.Show(ex1.Message); }
All Replies
-
Wednesday, March 07, 2012 12:51 PM
SMO often uses nested exceptions. I would suggest getting the base exception message as this typically provides a more helpful error message. See this MSDN page for instructions on SMO error handling:
- Marked As Answer by KJian_ Wednesday, March 14, 2012 2:26 AM
-
Wednesday, March 07, 2012 4:08 PM
I have found that windows server 2008 accepts passwords that are complex enough.
- Edited by Hichem Sharaeen Wednesday, March 07, 2012 4:10 PM
-
Saturday, March 10, 2012 7:16 PMModerator
Hello,
Just a little add-in to the excellent post of cmille19.
First, it would be more pleasant to put your code thru the Insert Code Block ( the button after this labelled HTML and you choose the good language C# ).
Before your catch ( Exception ... block, put a catch related to SmoException
/// <summary> /// Method to use when a SmoException occurs /// </summary> /// <param name="p_ex">SmoException to treat</param> public static void TreatSmoException(SmoException p_ex) { Exception _ex = null; Int32 p_i = 0; String p_s = ""; CommonCls.ErrorsCount++; CommonCls.ShortMessage = "SmoException in " + CommonCls.ErrorPlace; p_s = "********** (" + CommonCls.ErrorsCount.ToString() + ") " + CommonCls.NowToString() + CommonCls.ShortMessage + " **********" + CommonCls.Nl; p_s += "Message : " + p_ex.Message + CommonCls.Nl; p_s += "GetBaseException : " + p_ex.GetBaseException().GetType().FullName + CommonCls.Nl; p_s += "SmoExceptionType : " + Enum.GetName(typeof(SmoExceptionType),p_ex.SmoExceptionType) + CommonCls.Nl; p_s += "Source : " + p_ex.Source + CommonCls.Nl; p_s += "TargetSite : " + p_ex.TargetSite.Name + CommonCls.Nl; p_s += "Data : "; if (p_ex.Data == null) { p_s += "N/A" + CommonCls.Nl; } else { p_s += CommonCls.Nl; foreach (DictionaryEntry p_de in p_ex.Data) { p_s += " - Key : " + p_de.Key.ToString() + " Value : " + p_de.Value.ToString() + CommonCls.Nl; } } p_s += "InnerException : "; if (p_ex.InnerException == null) { p_s += "N/A" + CommonCls.Nl; } else { p_s += CommonCls.Nl; p_i = 0; _ex = p_ex.InnerException; while (_ex != null) { p_s += " - Inner" + p_i.ToString() + " : " + _ex.GetType().FullName + CommonCls.Nl; p_s += " Message : " + _ex.Message + CommonCls.Nl; _ex = _ex.InnerException; } } p_s += "StackTrace : " + CommonCls.Nl + p_ex.StackTrace + CommonCls.Nl; p_s += "********** " + CommonCls.NowToString() + " End of the " + CommonCls.ShortMessage + " **********" + CommonCls.Nl; CommonCls.ErrorMessage = p_s; CommonCls.AppWriteLog(false, p_s); }An example of the way i am treating exceptions :
try { .... } catch ( SmoException ex_smoexception ) { TreatSmoException(ex_smoexception); MessageBox.Show(CommonCls.ErrorMessage); } catch ( Exception ex_exception ) { TreatException(ex_exception); MessageBox.Show(CommonCls.ErrorMessage); }CommonCls is a library with only static methods/properties
CommonCls.ErrorMessage is defined as a public static String
CommonCls.AppWriteLog is a method which writes the 2nd parameter in an application logfile ( it is useful to retrieve the exact error message )
Don't hesitate to post again for more help or explanations.
Have a nive day
PS : i would suggest to study the InnerException ,Source,TargetSite of the classes derivated from the Exception class ( they give many useful informations, but don't forget some "special" properties like SmoExceptionType )
Mark Post as helpful if it provides any help.Otherwise,leave it as it is.

