2

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

该应用程序可以向具有到期日期的特定用户授予共享路径的读/写许可,并且所有这些数据都插入到 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();
}

任何想法都会很棒,谢谢

我最终这样做了,谢谢迈克尔..1

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);
}
4

1 回答 1

2

重新编写查询以获取这样的特定行

"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();
}

仔细查看上面的代码,您会发现我使用了 DataSet 并删除了 SqlReader,因为我发现 DataSet 更简单、更安全。

于 2012-09-18T13:40:50.213 回答