SQL Server CREATE USER 带参数 (VB.net) [英] SQL Server CREATE USER with Parameters (VB.net)

查看:28
本文介绍了SQL Server CREATE USER 带参数 (VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用给定的用户名(通过 sql 参数)执行 CREATE USER

I'm trying to execute CREATE USER with a given username (via sql parameter)

exec sp_executesql N'CREATE USER @LoginName 
                        FOR LOGIN @LoginName;',
                   N'@LoginName varchar(5)', @LoginName='myuser'

下面是生成上面的代码:

Heres the code thats generating the above:

Dim myCommand As SqlCommand = New SqlCommand("CREATE USER @LoginName 
                                                 FOR LOGIN @LoginName;", 
                                             ClassDatabaseConnection.CustomInstance)
myCommand.CommandType = CommandType.Text
myCommand.Parameters.Add("@LoginName", SqlDbType.VarChar).Value = LoginName
myCommand.ExecuteScalar()

我得到并出错:

Incorrect syntax near '@LoginName'.

我认为这是由于作为 VarChar 传递的参数导致MyUser"而不是 MyUser

I think this is due to the parameters being passed as VarChar causing 'MyUser' rather than MyUser

我不能用 sql 参数来做这个吗?

Can I not do this with sql parameters?

推荐答案

不能在 Create User 语句中使用参数.既然您已经在使用 VB,请尝试拼凑一个语句.

You cannot use parameters with a Create User statement. Since you're already in VB, try piecing together a statement.

Dim myCommand As SqlCommand = New SqlCommand("CREATE USER " + LoginName +
                                                " FOR LOGIN " + LoginName + ";", 
                                             ClassDatabaseConnection.CustomInstance)
myCommand.CommandType = CommandType.Text
myCommand.ExecuteScalar()

这篇关于SQL Server CREATE USER 带参数 (VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆