如何通过c#代码授予所有特权 [英] how to GRANT ALL PRIVILEGES by c# code

查看:90
本文介绍了如何通过c#代码授予所有特权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何通过c#在所有mysql数据库上将代码写入GRANT ALL PRIVILEGES给用户root#??

然后我去创建数据库并导入我的.sql文件



i尝试使用此代码但出现错误'主机'JE-PC'不允许连接到这个MySQL服务器'



how i can write code to GRANT ALL PRIVILEGES to user "root" on all mysql database by c# ??
then i went to create database and import my .sql file to it

i tried with this code but error appear 'Host 'JE-PC' is not allowed to connect to this MySQL server'

MySqlConnection connection001 = new MySqlConnection("Data Source=" + Publics.server + ";UserId=" + Publics.uid + ";PWD=" + Publics.password + "topSecretPassword;");
string query = "INSERT INTO users(user_name, password, admin) VALUES('" + Userdd + "','" + Pass + "', '1')";
MySqlCommand command = new MySqlCommand(query, connection001);
connection001.Open();
command.ExecuteNonQuery();


query = "CREATE USER '" + Userdd + "' IDENTIFIED BY '" + Pass + "'";
command = new MySqlCommand(query, connection001);
command.ExecuteNonQuery();

query = " grant all privileges on mydb.* to ttt8@'%' identified by 'ttt';";
command = new MySqlCommand(query, connection001);
command.ExecuteNonQuery();





谢谢。



thank you.

推荐答案

通过SQL脚本授予数据库权限。

因此,您要做的第一件事就是获得正确的语法。

您可以找到如何在 MySQL 5.7参考手册/ ... / GRANT语法 [< a href =http://dev.mysql.com/doc/refman/5.7/en/grant.html\"target =_ blank> ^ ]。



然后你必须得到如何从.NET程序集执行MySQL查询。

你将需要:

- 下载Connector/Net [ ^ ]。

- 查看 MySQL Connector / Net开发人员指南 [ ^ ] ,尤其是其第5章连接器/网络教程 [ ^ ]。



干得好:)



[更新]

您必须将用户定义为'用户名'@' computernameorip',而不仅仅是'用户名'。

在目前的情况下,这将给予

Granting rights on a database is done via a SQL script.
So, the first thing you have to do is to get the correct syntax.
You can find how to do that on MySQL 5.7 Reference Manual / ... / GRANT Syntax[^].

Then you have to get how to execute a MySQL query from a .NET assembly.
You will need:
- to download Connector/Net[^].
- to have a look at MySQL Connector/Net Developer Guide[^], and especially its Chapter 5 Connector/Net Tutorials[^].

Good work :)

[Update]
You have to define your user as 'username'@'computernameorip', not just 'username'.
In the present case, that would give
string computerNameOrIp = "%";
string login = string.Format("'{0}'@'{1}'", Userdd, computerNameOrIp); // login = 'username'@'%'
query = string.Format("CREATE USER {0} IDENTIFIED BY '{1}'", login, Pass);
command = new MySqlCommand(query, connection001);
command.ExecuteNonQuery();

query = string.Format("GRANT SELECT, SHOW DATABASES ON database.* TO {0}", login);
command = new MySqlCommand(query, connection001);
command.ExecuteNonQuery();



在MySQL中,每个元组有一个帐户(用户名,来源)。


In MySQL, there is one account per tuple (username, source).


这篇关于如何通过c#代码授予所有特权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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