使用存储过程创建MySql用户(服务器模式)

时间:2011-05-17 15:30:58

标签: mysql vb.net stored-procedures

我已成功创建用户名='root'和host ='127 .0.0.1',使用mysql命令(客户端模式)如下:

sql = String.Format("CREATE USER '{0}'@'{1}' IDENTIFIED BY '{2}'; GRANT ALL PRIVILEGES ON * . * TO '{0}'@'{1}' IDENTIFIED BY '{2}' WITH GRANT OPTION;", userName, serverHost, userPassword)

当上面的脚本我将其更改为存储过程(服务器模式)时,我发现错误如下:(错误1064 - 您的SQL语法有错误;请查看与Yout MySQL服务器版本对应的手册在第1行的'pass'附近使用正确的语法

ScriptExec(String.Format("DROP PROCEDURE IF EXISTS mySchema.SP_CreateUser $$" +
                         "CREATE DEFINER=root@localhost PROCEDURE {0}.SP_CreateUser(IN userName VARCHAR(250), IN serverHost VARCHAR(250), IN userPassword VARCHAR(250)) " +
                         "BEGIN " +
                         "SET @buf = CONCAT('CREATE USER ',userName,'@',serverHost,' IDENTIFIED BY ',userPassword); PREPARE stmt FROM @buf; EXECUTE stmt; DEALLOCATE PREPARE stmt;" +
                         "SET @buf1 = CONCAT('GRANT ALL PRIVILEGES ON * . * TO ',userName,'@',serverHost,' IDENTIFIED BY ',userPassword,' WITH GRANT OPTION'); PREPARE stmt1 FROM @buf1; EXECUTE stmt1; DEALLOCATE PREPARE stmt1;" +
                         "END $$"))

Public Function ScriptExec(ByVal sql As String) As Boolean
    Dim stsResult As Boolean = Nothing
    Dim has As Integer = Nothing
    Dim script As New MySqlScript(objCon, sql)
    script.Delimiter = "$$"
    Try
        has = script.Execute()
        If has > 0 Then stsResult = True
    Catch ex As MySqlException
        MessageBox.Show("Error occurred in ScriptExec Function" & vbCrLf & ex.Number & " – " & ex.Message)
    Finally
        script.Delimiter = ";"
    End Try
    Return stsResult
End Function

上面的问题是否有解决方案?

谢谢&最诚挚的问候,

Yudha

1 个答案:

答案 0 :(得分:0)

你不是错过了

DELIMITER $$
SQL代码开头的

语句?没有它,引擎将不知道如何解释$$