Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用mysql结果构造C#actions_C#_Mysql - Fatal编程技术网

用mysql结果构造C#actions

用mysql结果构造C#actions,c#,mysql,C#,Mysql,我有一个mysql数据库,包含用户名、日期和共享路径等数据 该应用程序可以向特定用户授予共享路径的r/w许可,该许可具有到期日期,并且所有数据都插入mysql 我希望能够像一段时间一样逐行读取,每个循环读取每一行,如果找到exp date=today的行,则获取用户和共享路径,并删除许可证 try { string MyConString = "SERVER=localhost;" + "DATABASE=permdata;" + "UID

我有一个mysql数据库,包含用户名、日期和共享路径等数据

该应用程序可以向特定用户授予共享路径的r/w许可,该许可具有到期日期,并且所有数据都插入mysql

我希望能够像一段时间一样逐行读取,每个循环读取每一行,如果找到exp date=today的行,则获取用户和共享路径,并删除许可证

try
{
    string MyConString =
        "SERVER=localhost;" +
        "DATABASE=permdata;" +
        "UID=user;" +
        "PASSWORD=pass;";

    string sql = "SELECT expDate, user, sfolder FROM  permuser";

    MySqlConnection connection = new MySqlConnection(MyConString);
    MySqlCommand cmdSel = new MySqlCommand(sql, connection);
    MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
    MySqlDataReader dr = cmdSel.ExecuteReader();

    while  (dr.Read())
    {
        foreach ( *row that exp. date = today)
        {
            *get user and share path
            *myDirectorySecurity.RemoveAccessRule (user/share path)
        }
    }

    connection.Close();
    connection.Dispose();
}

catch (MySqlException ex)
{
    MessageBox.Show(ex.Message);
    Close();
}
有什么好主意都行,谢谢

最后我做了这件事,谢谢迈克尔

try
        {
string MyConString =
    "SERVER=localhost;" +
    "DATABASE=permdata;" +
    "UID=user;" +
    "PASSWORD=pass;";

DataSet dataSet = new DataSet(); 
string sql = "SELECT key, fdate, user, perm, sfolder FROM  permuser WHERE fdate=CURDATE()";

MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand cmdSel = new MySqlCommand(sql, connection);
new MySqlDataAdapter(cmdSel).Fill(dataSet, "permuser");


foreach (DataRow row in dataSet.Tables["permuser"].Rows)
    {
         string fuser = row["user"].ToString();
         string pathtxt = row["sfolder"].ToString();

        DirectoryInfo myDirectoryInfo = new DirectoryInfo(pathtxt);
        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
        string User = System.Environment.UserDomainName + "\\" + fuser;


                myDirectorySecurity.RemoveAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write | FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
     PropagationFlags.None, AccessControlType.Allow));

                myDirectoryInfo.SetAccessControl(myDirectorySecurity);
    }

connection.Close();
connection.Dispose();
}


catch (MySqlException ex)
{
    Console.WriteLine(ex.Message);
   Environment.Exit(0);
}

重新编写查询以获取如下所示的特定行

"SELECT expDate, user, sfolder FROM  permuser WHERE expDate=GetDate()";
我已将您的代码重新调整如下

try
{
    string MyConString =
        "SERVER=localhost;" +
        "DATABASE=permdata;" +
        "UID=user;" +
        "PASSWORD=pass;";

    DataSet dataSet = new DataSet(); // Set a new DataSet instance
    string sql = "SELECT expDate, user, sfolder FROM  permuser WHERE expDate=GetDate()"

    MySqlConnection connection = new MySqlConnection(MyConString);
    MySqlCommand cmdSel = new MySqlCommand(sql, connection);
    new SqlDataAdapter(cmdSel).Fill(dataSet, "permuser"); // passing the command into the DataAdapter


    foreach (DataRow row in dataSet.Tables["permuser"].Rows)
        {
            //*get user and share path
            row["user"]; //User rows
             row["sfolder"]; //sfolder rows
            *myDirectorySecurity.RemoveAccessRule (user/share path)
        }

    connection.Close();
    connection.Dispose();
}

catch (MySqlException ex)
{
    MessageBox.Show(ex.Message);
    Close();
}

仔细看一下上面的代码,您会发现我使用了一个数据集,并删除了SqlReader,因为我发现数据集更容易、更安全

有了这些,我就得到了今天日期的所有行。因此,我可以将该操作应用于集合中的每一行。那么foreach循环可以使用什么?[用户]中的foreach用户?您发现Michael的答案中有什么不起作用或是OK?