check to see if a login exists in sql from c# -
within class creating log-ins sql works fine. wanted add little error checking first check see if login existing in sql. if don't try add login map tables etc.
in sql check login using following:
select name sys.server_principals name = 'viewer2' i tired use in class follows
protected static bool checkforexistinguser(dbcontext context, string username) { string checkforuser = @" set nocount on declare @username nvarchar(max) = {0} exec(' select name sys.server_principals name = ['+ @username +'] ')"; return convert.toboolean(context.database.executesqlcommand(checkforuser, username)); } however when call method exception column (being whatever username passed in) invalid
$exception {"invalid column name 'viewer2'."} system.exception {system.data.sqlclient.sqlexception} any ideas causing , there better way check see if login exists within sql db code?
cheers
you should use ' instead of [] around value. otherwise, sql server treat column name.
string checkforuser = @" set nocount on declare @username nvarchar(max) = {0} exec(' select name sys.server_principals name = ''' + @username +''' ')";
Comments
Post a Comment