I want to create a database User in C#. In Microsoft SQL Server Management Studio I just can create one with the following command:
CREATE USER [DOMAIN\user]
But when I try to do the same in C# I can't send a single backslash in the command.
string query = @"CREATE USER [DOMAIN\user]";
I already tried things like query.Replace("\\\\", "\\")
or query.Replace(@"\\\\", @"\\")
, but nothing worked for me. I always get an error with:
Incorrect syntax near '\\'.
It's the same like I would write CREATE USER [DOMAIN\\\\user]
in SQL Server Management Studio.
Somehow I can't transfer just one backslash in the query.
Does anyone have a hint for me?
Thats my actual code:
public SqlConnection Conn;
public bool CreateUser(string LoginName)
{
string dom_login = Domain + "\\" + LoginName;
string query = @"CREATE USER [" + dom_login.Replace("\\",@"\") + "]";
// now query should be okay, so send to sql connection
DataTable dt = new DataTable();
if (Conn.State == ConnectionState.Closed)
Conn.Open();
try
{
SqlDataAdapter sDataAsp = new SqlDataAdapter(query, Conn);
sDataAsp.Fill(dt);
}
catch (Exception ex)
{
// Here i get the syntax error
string error = ex.Message;
return false;
}
return true;
}